Source for file time.class.php

Documentation is available at time.class.php


1 <?php
2 /**
3 *
4 * Time class and other time funktions
5 *
6 * @category phpTimeSheet
7 * @package Time
8 * @version $Id: time.class.php,v 1.15 2003/09/23 16:11:30 cybot_tm Exp $
9 * @author Sebastian Mendel <info@sebastianmendel.de>
10 */
11
12 /**
13 *
14 */
15 define('TIME_OMIT_SIGN', 1);
16 define('TIME_FORCE_SIGN', 2);
17 define('TIME_OMIT_NULL', 4);
18
19 /**
20 * Class Time
21 *
22 * @package Time
23 */
24 class Time
25 {
26 /**
27 * @var int Hours
28 * @access protected
29 * @since v1.0
30 */
31 var $hours = 0;
32
33 /**
34 * @var int Minutes
35 * @access protected
36 * @since v1.0
37 */
38 var $minutes = 0;
39
40 /**
41 * @var int Seconds
42 * @access protected
43 * @since v1.0
44 */
45 var $seconds = 0;
46
47 /**
48 * @var int Microseconds
49 * @access protected
50 * @since v1.0
51 */
52 var $fractals = 0;
53
54 /**
55 * @var int sign (-1, +1)
56 * @access protected
57 * @since v1.0
58 */
59 var $sign = 1;
60
61 /**
62 * expects Time in form of seconds as int
63 * or in form H:M[:S[.M]] as string
64 * or null, if $time is is null then current time is taken
65 *
66 * @uses Time::Set()
67 * @param mixed Time
68 * @return bool success
69 * @since v1.0
70 */
71 function Time($time = null)
72 {
73 $this->Set($time);
74 return true;
75 }
76
77 /**
78 * returns time in form HH:MM
79 *
80 * @return string time
81 * @since v1.0
82 */
83 function Get($display_sign = null)
84 {
85 if ( $display_sign !== TIME_OMIT_SIGN && $this->GetSign() === -1 )
86 {
87 $sign = '-';
88 }
89 elseif ( $display_sign === TIME_FORCE_SIGN && $this->GetSign() === 1 )
90 {
91 $sign = '+';
92 }
93 else
94 {
95 $sign = '';
96 }
97 return $sign . sprintf("%02d:%02d", $this->GetHours(), $this->GetMinutes());
98 }
99
100 function GetColored($display_sign = null)
101 {
102 if ( $this->GetSign() === -1 )
103 {
104 return '<span style="color: #ff0000">' . $this->Get($display_sign) . '</span>';
105 }
106 else
107 {
108 return '<span style="color: #00ff00">' . $this->Get($display_sign) . '</span>';
109 }
110 }
111
112 /**
113 * returns hours
114 *
115 * @return int hours
116 * @since v1.0
117 */
118 function GetHours() { return $this->hours; }
119
120 /**
121 * returns minutes
122 *
123 * @return int minutes
124 * @since v1.0
125 */
126 function GetMinutes() { return $this->minutes; }
127
128 /**
129 * returns seconds fo time
130 *
131 * @return int seconds
132 * @since v1.4
133 */
134 function GetSeconds() { return $this->seconds; }
135
136 /**
137 * returns sign as -1 or +1
138 *
139 * @return int sign
140 * @since v1.4
141 */
142 function GetSign() { return $this->sign; }
143
144 /**
145 * returns time in seconds
146 *
147 * @return int seconds
148 * @since v1.4
149 */
150 function GetAsSeconds()
151 {
152 return $this->GetSign() * (($this->GetHours() * 60 * 60) + ($this->GetMinutes() * 60) + ($this->GetSeconds()));
153 }
154
155 /**
156 * sets sign, returns true on success, otherwise false
157 *
158 * @param int sign
159 * @return bool success
160 * @since v1.0
161 */
162 function SetSign($sign)
163 {
164 $this->sign = (int) $sign;
165 return true;
166 }
167
168 /**
169 * sets hours, returns true on success, otherwise false
170 *
171 * @param int hours
172 * @return bool success
173 * @since v1.0
174 */
175 function SetHours($hours)
176 {
177 $this->hours = (int) $hours;
178 return true;
179 }
180
181 /**
182 * sets minutes, returns true on success, otherwise false
183 *
184 * @param int minutes
185 * @return bool success
186 * @since v1.0
187 */
188 function SetMinutes($minutes)
189 {
190 $this->minutes = (int) $minutes;
191 return true;
192 }
193
194 /**
195 * sets seconds, returns true on success, otherwise false
196 *
197 * @param int seconds
198 * @return bool success
199 * @since v1.0
200 */
201 function SetSeconds($seconds)
202 {
203 $this->seconds = (int) $seconds;
204 return true;
205 }
206
207 /**
208 * @param mixed Time
209 * @return bool success
210 * @since v1.0
211 */
212 function Set($time)
213 {
214 if ( is_object($time) && get_class($time) == 'time' )
215 {
216 $this = $time;
217 }
218 elseif ( preg_match('|:|', $time) )
219 {
220 return $this->SetTimeFromString($time);
221 }
222 else
223 {
224 return $this->SetTimeFromSeconds($time);
225 }
226 }
227
228 /**
229 * sets time from given seconds, returns true on success, otherwise false
230 *
231 * @param int seconds
232 * @return bool success
233 * @since v1.0
234 */
235 function SetTimeFromSeconds($seconds)
236 {
237 $seconds = (int) $seconds;
238
239 if ( $seconds < 0 )
240 {
241 $seconds = $seconds * -1;
242 $this->SetSign(-1);
243 }
244 else
245 {
246 $this->SetSign(1);
247 }
248
249 $this->SetHours(floor($seconds / 60 / 60));
250 $seconds = $seconds % ( 60 * 60 );
251 $this->SetMinutes(floor($seconds / 60 ));
252 $seconds = $seconds % 60;
253 $this->SetSeconds($seconds);
254
255 return true;
256 }
257
258 /**
259 * sets time from string in form HH:MM(:SS)
260 * returns true on success, otherwise false
261 *
262 * @param int seconds
263 * @return bool success
264 * @since v1.0
265 */
266 function SetTimeFromString($time)
267 {
268 preg_match("/^(\-)?([0-9]{1,2}):([0-9]{1,2})(:([0-9]{1,2}))?$/", $time, $time_split);
269
270 if ( $time_split[1] == '-' )
271 {
272 $this->SetSign(-1);
273 }
274 else
275 {
276 $this->SetSign(1);
277 }
278
279 $this->SetHours($time_split[2]);
280 if ( isset($time_split[3]) )
281 {
282 $this->SetMinutes($time_split[3]);
283 }
284
285 if ( isset($time_split[5]) )
286 {
287 $this->SetSeconds($time_split[5]);
288 }
289
290 return true;
291 }
292
293 function IsNull()
294 {
295 if ( $this->GetHours() + $this->GetMinutes() + $this->GetSeconds() === 0 )
296 {
297 return true;
298 }
299
300 return false;
301 }
302
303 /**
304 * Adds time
305 *
306 * @param mixed Time
307 * @return bool success
308 */
309 function Add($time)
310 {
311 $mytime = new Time($time);
312 $new_time = $this->GetAsSeconds() + $mytime->GetAsSeconds();
313 $this->SetTimeFromSeconds($new_time);
314 return true;
315 }
316
317 /**
318 * @param mixed Time
319 * @return bool success
320 */
321 function Sub($time)
322 {
323 $mytime = new Time($time);
324 $new_time = $this->GetAsSeconds() - $mytime->GetAsSeconds();
325 $this->SetTimeFromSeconds($new_time);
326 return true;
327 }
328
329 /**
330 * @param mixed Time
331 * @return bool success
332 */
333 function Mul($time)
334 {
335 return true;
336 }
337
338 /**
339 * @param mixed Time
340 * @return bool success
341 */
342 function Div($time)
343 {
344 return true;
345 }
346
347 /**
348 * @param mixed Time
349 * @return string time
350 */
351 function Diff($time)
352 {
353 return true;
354 }
355 }
356
357 function is_time($time)
358 {
359 if ( ! preg_match("/^(\-)?[0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2})?$/", $time) )
360 {
361 return false;
362 }
363
364 return true;
365 }
366
367 function is_valid_time($time)
368 {
369 return is_time($time);
370 }
371
372 function _time_to_seconds($time)
373 {
374 preg_match("/^(\-)?([0-9]{1,2}):([0-9]{1,2})(:([0-9]{1,2}))?$/", $time, $time_split);
375
376 $time_pre = $time_split[1];
377 $time_hours = $time_split[2];
378 $time_minutes = $time_split[3];
379 $time_seconds = $time_split[5];
380
381 $time_in_seconds = $time_seconds + $time_minutes * 60 + $time_hours * 60 * 60;
382 if ( $time_pre == '-' )
383 {
384 $time_in_seconds = $time_in_seconds * -1;
385 }
386
387 return (int) $time_in_seconds;
388 }
389
390 function seconds_to_time($time_in_seconds)
391 {
392 if ( $time_in_seconds < 0 )
393 {
394 $time_in_seconds = $time_in_seconds * -1;
395 $pre = '-';
396 }
397 else
398 {
399 $pre = '';
400 }
401
402 $time_hours = (int) ($time_in_seconds / 60 / 60);
403 $time_in_seconds = $time_in_seconds % ( 60 * 60 );
404 $time_minutes = (int) ($time_in_seconds / 60 );
405 $time_in_seconds = $time_in_seconds % 60;
406 $time_seconds = (int) ($time_in_seconds);
407
408 return $pre . str_pad($time_hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($time_minutes, 2, '0', STR_PAD_LEFT) . ':' . str_pad($time_seconds, 2, '0', STR_PAD_LEFT);
409 }
410
411 function _time_sub($time1, $time2)
412 {
413 if ( empty($time1) )
414 {
415 $time1 = '00:00';
416 }
417
418 if ( empty($time2) )
419 {
420 $time2 = '00:00';
421 }
422
423 if ( ! is_valid_time($time1) || ! is_valid_time($time2) )
424 {
425 return false;
426 }
427
428 $time = seconds_to_time(time_to_seconds($time1) - time_to_seconds($time2));
429
430 if ( strlen($time1) > 6 || strlen($time2) > 6 )
431 {
432 return $time;
433 }
434 else
435 {
436 // cut off seconds
437 return substr($time, 0, -3);
438 }
439 }
440
441 function time_add($time1, $time2)
442 {
443 if ( empty($time1) )
444 {
445 $time1 = '00:00';
446 }
447
448 if ( empty($time2) )
449 {
450 $time2 = '00:00';
451 }
452
453 if ( ! is_valid_time($time1) || ! is_valid_time($time2) )
454 {
455 return false;
456 }
457
458 $time = seconds_to_time(time_to_seconds($time1) + time_to_seconds($time2));
459
460 if ( strlen($time1) > 6 || strlen($time2) > 6 )
461 {
462 return $time;
463 }
464 else
465 {
466 // cut off seconds
467 return substr($time, 0, -3);
468 }
469 }
470 ?>

Documentation generated on Fri, 26 Sep 2003 15:39:57 +0200 by phpDocumentor 1.2.2