2 // vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Leandro Lucarella <llucax@php.net> |
17 // +----------------------------------------------------------------------+
22 require_once 'Date.php';
23 require_once 'Date/Calc.php';
26 * Non Numeric Separated Values (NNSV) Input Format.
28 * Input format guessed from something like this:
29 * days<sep>hours<sep>minutes<sep>seconds
30 * Where <sep> is any quantity of non numeric chars. If no values are
31 * given, time span is set to zero, if one value is given, it's used for
32 * hours, if two values are given it's used for hours and minutes and if
33 * three values are given, it's used for hours, minutes and seconds.<br>
35 * '' -> 0, 0, 0, 0 (days, hours, minutes, seconds)<br>
37 * '12.30' -> 0, 12, 30, 0<br>
38 * '12:30:18' -> 0, 12, 30, 18<br>
39 * '3-12-30-18' -> 3, 12, 30, 18<br>
40 * '3 days, 12-30-18' -> 3, 12, 30, 18<br>
41 * '12:30 with 18 secs' -> 0, 12, 30, 18<br>
45 define('DATE_SPAN_INPUT_FORMAT_NNSV', 1);
48 * Default time format when converting to a string.
52 $_DATE_SPAN_FORMAT = '%C';
55 * Default time format when converting from a string.
59 $_DATE_SPAN_INPUT_FORMAT = DATE_SPAN_INPUT_FORMAT_NNSV;
62 * Generic time span handling class for PEAR.
65 * @author Leandro Lucarella <llucax@php.net>
66 * @version $Revision: 1.4 $
68 * @todo Get and set default local input and output formats?
96 * Creates the time span object calling the set() method.
98 * @param mixed $time Time span expression.
99 * @param mixed $format Format string to set it from a string or the
100 * second date set it from a date diff.
105 function Date_Span($time = 0, $format = null)
107 $this->set($time, $format);
111 * Set the time span to a new value in a 'smart' way.
113 * Sets the time span depending on the argument types, calling
114 * to the appropriate setFromXxx() method.
116 * @param mixed $time Time span expression.
117 * @param mixed $format Format string to set it from a string or the
118 * second date set it from a date diff.
120 * @return bool true on success.
122 * @see setFromObject()
123 * @see setFromArray()
124 * @see setFromString()
125 * @see setFromSeconds()
126 * @see setFromDateDiff()
129 function set($time = 0, $format = null)
131 if (is_a($time, 'date_span')) {
132 return $this->copy($time);
133 } elseif (is_a($time, 'date') and is_a($format, 'date')) {
134 return $this->setFromDateDiff($time, $format);
135 } elseif (is_array($time)) {
136 return $this->setFromArray($time);
137 } elseif (is_string($time)) {
138 return $this->setFromString($time, $format);
139 } elseif (is_int($time)) {
140 return $this->setFromSeconds($time);
142 return $this->setFromSeconds(0);
147 * Set the time span from an array.
149 * Set the time span from an array. Any value can be a float (but it
150 * has no sense in seconds), for example array(23.5, 20, 0) is
151 * interpreted as 23 hours, .5*60 + 20 = 50 minutes and 0 seconds.
153 * @param array $time Items are counted from right to left. First
154 * item is for seconds, second for minutes, third
155 * for hours and fourth for days. If there are
156 * less items than 4, zero (0) is assumed for the
159 * @return bool True on success.
163 function setFromArray($time)
165 if (!is_array($time)) {
168 $tmp1 = new Date_Span;
169 if (!$tmp1->setFromSeconds(@array_pop($time))) {
172 $tmp2 = new Date_Span;
173 if (!$tmp2->setFromMinutes(@array_pop($time))) {
177 if (!$tmp2->setFromHours(@array_pop($time))) {
181 if (!$tmp2->setFromDays(@array_pop($time))) {
185 return $this->copy($tmp1);
189 * Set the time span from a string based on an input format.
191 * Set the time span from a string based on an input format. This is
192 * some like a mix of format() method and sscanf() PHP function. The
193 * error checking and validation of this function is very primitive,
194 * so you should be carefull when using it with unknown $time strings.
195 * With this method you are assigning day, hour, minute and second
196 * values, and the last values are used. This means that if you use
197 * something like setFromString('10, 20', '%H, %h') your time span
198 * would be 20 hours long. Allways remember that this method set
199 * <b>all</b> the values, so if you had a $time span 30 minutes long
200 * and you make $time->setFromString('20 hours', '%H hours'), $time
201 * span would be 20 hours long (and not 20 hours and 30 minutes).
202 * Input format options:<br>
203 * <code>%C</code> Days with time, same as "%D, %H:%M:%S".<br>
204 * <code>%d</code> Total days as a float number
205 * (2 days, 12 hours = 2.5 days).<br>
206 * <code>%D</code> Days as a decimal number.<br>
207 * <code>%e</code> Total hours as a float number
208 * (1 day, 2 hours, 30 minutes = 26.5 hours).<br>
209 * <code>%f</code> Total minutes as a float number
210 * (2 minutes, 30 seconds = 2.5 minutes).<br>
211 * <code>%g</code> Total seconds as a decimal number
212 * (2 minutes, 30 seconds = 90 seconds).<br>
213 * <code>%h</code> Hours as decimal number.<br>
214 * <code>%H</code> Hours as decimal number limited to 2 digits.<br>
215 * <code>%m</code> Minutes as a decimal number.<br>
216 * <code>%M</code> Minutes as a decimal number limited to 2 digits.<br>
217 * <code>%n</code> Newline character (\n).<br>
218 * <code>%p</code> Either 'am' or 'pm' depending on the time. If 'pm'
219 * is detected it adds 12 hours to the resulting time
220 * span (without any checks). This is case
222 * <code>%r</code> Time in am/pm notation, same as "%H:%M:%S %p".<br>
223 * <code>%R</code> Time in 24-hour notation, same as "%H:%M".<br>
224 * <code>%s</code> Seconds as a decimal number.<br>
225 * <code>%S</code> Seconds as a decimal number limited to 2 digits.<br>
226 * <code>%t</code> Tab character (\t).<br>
227 * <code>%T</code> Current time equivalent, same as "%H:%M:%S".<br>
228 * <code>%%</code> Literal '%'.<br>
230 * @param string $time String from where to get the time span
232 * @param string $format Format string.
234 * @return bool True on success.
238 function setFromString($time, $format = null)
240 if (is_null($format)) {
241 $format = $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
243 // If format is a string, it parses the string format.
244 if (is_string($format)) {
248 $day = $hour = $minute = $second = 0;
249 for ($i = 0; $i < strlen($format); $i++) {
252 $nextchar = $format{++$i};
255 $str .= '%d, %d:%d:%d';
257 $vars, 'day', 'hour', 'minute', 'second');
260 $str .= '%d, %2d:%2d:%2d';
262 $vars, 'day', 'hour', 'minute', 'second');
266 array_push($vars, 'day');
270 array_push($vars, 'day');
274 array_push($vars, 'hour');
278 array_push($vars, 'minute');
282 array_push($vars, 'second');
286 array_push($vars, 'hour');
290 array_push($vars, 'hour');
294 array_push($vars, 'minute');
298 array_push($vars, 'minute');
305 array_push($vars, 'pm');
308 $str .= '%2d:%2d:%2d %2s';
310 $vars, 'hour', 'minute', 'second', 'pm');
314 array_push($vars, 'hour', 'minute');
318 array_push($vars, 'second');
322 array_push($vars, 'second');
328 $str .= '%2d:%2d:%2d';
329 array_push($vars, 'hour', 'minute', 'second');
335 $str .= $char . $nextchar;
341 $vals = sscanf($time, $str);
342 foreach ($vals as $i => $val) {
348 if (strcasecmp($pm, 'pm') == 0) {
350 } elseif (strcasecmp($pm, 'am') != 0) {
353 $this->setFromArray(array($day, $hour, $minute, $second));
354 // If format is a integer, it uses a predefined format
356 } elseif (is_integer($format)) {
358 case DATE_SPAN_INPUT_FORMAT_NNSV:
359 $time = preg_split('/\D+/', $time);
360 switch (count($time)) {
362 return $this->setFromArray(
365 return $this->setFromArray(
366 array(0, $time[0], 0, 0));
368 return $this->setFromArray(
369 array(0, $time[0], $time[1], 0));
371 return $this->setFromArray(
372 array(0, $time[0], $time[1], $time[2]));
374 return $this->setFromArray($time);
383 * Set the time span from a total number of seconds.
385 * @param int $seconds Total number of seconds.
387 * @return bool True on success.
391 function setFromSeconds($seconds)
396 $sec = intval($seconds);
397 $min = floor($sec / 60);
398 $hour = floor($min / 60);
399 $day = intval(floor($hour / 24));
400 $this->second = $sec % 60;
401 $this->minute = $min % 60;
402 $this->hour = $hour % 24;
408 * Set the time span from a total number of minutes.
410 * @param float $minutes Total number of minutes.
412 * @return bool True on success.
416 function setFromMinutes($minutes)
418 return $this->setFromSeconds(round($minutes * 60));
422 * Set the time span from a total number of hours.
424 * @param float $hours Total number of hours.
426 * @return bool True on success.
430 function setFromHours($hours)
432 return $this->setFromSeconds(round($hours * 3600));
436 * Set the time span from a total number of days.
438 * @param float $days Total number of days.
440 * @return bool True on success.
444 function setFromDays($days)
446 return $this->setFromSeconds(round($days * 86400));
450 * Set the span from the elapsed time between two dates.
452 * Set the span from the elapsed time between two dates. The time span
453 * is allways positive, so the date's order is not important.
455 * @param object Date $date1 First Date.
456 * @param object Date $date2 Second Date.
458 * @return bool True on success.
462 function setFromDateDiff($date1, $date2)
464 if (!is_a($date1, 'date') or !is_a($date2, 'date')) {
469 if ($date1->after($date2)) {
470 list($date1, $date2) = array($date2, $date1);
472 $days = Date_Calc::dateDiff(
473 $date1->getDay(), $date1->getMonth(), $date1->getYear(),
474 $date2->getDay(), $date2->getMonth(), $date2->getYear()
476 $hours = $date2->getHour() - $date1->getHour();
477 $mins = $date2->getMinute() - $date1->getMinute();
478 $secs = $date2->getSecond() - $date1->getSecond();
479 $this->setFromSeconds(
480 $days * 86400 + $hours * 3600 + $mins * 60 + $secs
486 * Set the time span from another time object.
488 * @param object Date_Span $time Source time span object.
490 * @return bool True on success.
496 if (is_a($time, 'date_span')) {
497 $this->second = $time->second;
498 $this->minute = $time->minute;
499 $this->hour = $time->hour;
500 $this->day = $time->day;
508 * Time span pretty printing (similar to Date::format()).
510 * Formats the time span in the given format, similar to
511 * strftime() and Date::format().<br>
513 * Formatting options:<br>
514 * <code>%C</code> Days with time, same as "%D, %H:%M:%S".<br>
515 * <code>%d</code> Total days as a float number
516 * (2 days, 12 hours = 2.5 days).<br>
517 * <code>%D</code> Days as a decimal number.<br>
518 * <code>%e</code> Total hours as a float number
519 * (1 day, 2 hours, 30 minutes = 26.5 hours).<br>
520 * <code>%E</code> Total hours as a decimal number
521 * (1 day, 2 hours, 40 minutes = 26 hours).<br>
522 * <code>%f</code> Total minutes as a float number
523 * (2 minutes, 30 seconds = 2.5 minutes).<br>
524 * <code>%F</code> Total minutes as a decimal number
525 * (1 hour, 2 minutes, 40 seconds = 62 minutes).<br>
526 * <code>%g</code> Total seconds as a decimal number
527 * (2 minutes, 30 seconds = 90 seconds).<br>
528 * <code>%h</code> Hours as decimal number (0 to 23).<br>
529 * <code>%H</code> Hours as decimal number (00 to 23).<br>
530 * <code>%i</code> Hours as decimal number on 12-hour clock
532 * <code>%I</code> Hours as decimal number on 12-hour clock
534 * <code>%m</code> Minutes as a decimal number (0 to 59).<br>
535 * <code>%M</code> Minutes as a decimal number (00 to 59).<br>
536 * <code>%n</code> Newline character (\n).<br>
537 * <code>%p</code> Either 'am' or 'pm' depending on the time.<br>
538 * <code>%P</code> Either 'AM' or 'PM' depending on the time.<br>
539 * <code>%r</code> Time in am/pm notation, same as "%I:%M:%S %p".<br>
540 * <code>%R</code> Time in 24-hour notation, same as "%H:%M".<br>
541 * <code>%s</code> Seconds as a decimal number (0 to 59).<br>
542 * <code>%S</code> Seconds as a decimal number (00 to 59).<br>
543 * <code>%t</code> Tab character (\t).<br>
544 * <code>%T</code> Current time equivalent, same as "%H:%M:%S".<br>
545 * <code>%%</code> Literal '%'.<br>
547 * @param string $format The format string for returned time span.
549 * @return string The time span in specified format.
553 function format($format = null)
555 if (is_null($format)) {
556 $format = $GLOBALS['_DATE_SPAN_FORMAT'];
559 for ($i = 0; $i < strlen($format); $i++) {
562 $nextchar = $format{++$i};
566 '%d, %02d:%02d:%02d',
574 $output .= $this->toDays();
577 $output .= $this->day;
580 $output .= $this->toHours();
583 $output .= floor($this->toHours());
586 $output .= $this->toMinutes();
589 $output .= floor($this->toMinutes());
592 $output .= $this->toSeconds();
595 $output .= $this->hour;
598 $output .= sprintf('%02d', $this->hour);
602 ($this->hour + 1) > 12 ?
605 $output .= ($hour == 0) ? 12 : $hour;
609 ($this->hour + 1) > 12 ?
612 $output .= sprintf('%02d', $hour==0 ? 12 : $hour);
615 $output .= $this->minute;
618 $output .= sprintf('%02d',$this->minute);
624 $output .= $this->hour >= 12 ? 'pm' : 'am';
627 $output .= $this->hour >= 12 ? 'PM' : 'AM';
631 ($this->hour + 1) > 12 ?
636 $hour==0 ? 12 : $hour,
639 $this->hour >= 12 ? 'pm' : 'am'
644 '%02d:%02d', $this->hour, $this->minute
648 $output .= $this->second;
651 $output .= sprintf('%02d', $this->second);
659 $this->hour, $this->minute, $this->second
666 $output .= $char . $nextchar;
676 * Convert time span to seconds.
678 * @return int Time span as an integer number of seconds.
684 return $this->day * 86400 + $this->hour * 3600 +
685 $this->minute * 60 + $this->second;
689 * Convert time span to minutes.
691 * @return float Time span as a decimal number of minutes.
697 return $this->day * 1440 + $this->hour * 60 + $this->minute +
702 * Convert time span to hours.
704 * @return float Time span as a decimal number of hours.
710 return $this->day * 24 + $this->hour + $this->minute / 60 +
711 $this->second / 3600;
715 * Convert time span to days.
717 * @return float Time span as a decimal number of days.
723 return $this->day + $this->hour / 24 + $this->minute / 1440 +
724 $this->second / 86400;
730 * @param object Date_Span $time Time span to add.
736 return $this->setFromSeconds(
737 $this->toSeconds() + $time->toSeconds()
742 * Subtracts a time span.
744 * Subtracts a time span. If the time span to subtract is larger
745 * than the original, the result is zero (there's no sense in
746 * negative time spans).
748 * @param object Date_Span $time Time span to subtract.
752 function subtract($time)
754 $sub = $this->toSeconds() - $time->toSeconds();
756 $this->setFromSeconds(0);
758 $this->setFromSeconds($sub);
763 * Tells if time span is equal to $time.
765 * @param object Date_Span $time Time span to compare to.
767 * @return bool True if the time spans are equal.
771 function equal($time)
773 return $this->toSeconds() == $time->toSeconds();
777 * Tells if this time span is greater or equal than $time.
779 * @param object Date_Span $time Time span to compare to.
781 * @return bool True if this time span is greater or equal than $time.
785 function greaterEqual($time)
787 return $this->toSeconds() >= $time->toSeconds();
791 * Tells if this time span is lower or equal than $time.
793 * @param object Date_Span $time Time span to compare to.
795 * @return bool True if this time span is lower or equal than $time.
799 function lowerEqual($time)
801 return $this->toSeconds() <= $time->toSeconds();
805 * Tells if this time span is greater than $time.
807 * @param object Date_Span $time Time span to compare to.
809 * @return bool True if this time span is greater than $time.
813 function greater($time)
815 return $this->toSeconds() > $time->toSeconds();
819 * Tells if this time span is lower than $time.
821 * @param object Date_Span $time Time span to compare to.
823 * @return bool True if this time span is lower than $time.
827 function lower($time)
829 return $this->toSeconds() < $time->toSeconds();
833 * Compares two time spans.
835 * Compares two time spans. Suitable for use in sorting functions.
837 * @param object Date_Span $time1 The first time span.
838 * @param object Date_Span $time2 The second time span.
840 * @return int 0 if the time spans are equal, -1 if time1 is lower
841 * than time2, 1 if time1 is greater than time2.
846 function compare($time1, $time2)
848 if ($time1->equal($time2)) {
850 } elseif ($time1->lower($time2)) {
858 * Tells if the time span is empty (zero length).
860 * @return bool True is it's empty.
864 return !$this->day && !$this->hour && !$this->minute && !$this->second;
868 * Set the default input format.
870 * @param mixed $format New default input format.
872 * @return mixed Previous default input format.
876 function setDefaultInputFormat($format)
878 $old = $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
879 $GLOBALS['_DATE_SPAN_INPUT_FORMAT'] = $format;
884 * Get the default input format.
886 * @return mixed Default input format.
890 function getDefaultInputFormat()
892 return $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
896 * Set the default format.
898 * @param mixed $format New default format.
900 * @return mixed Previous default format.
904 function setDefaultFormat($format)
906 $old = $GLOBALS['_DATE_SPAN_FORMAT'];
907 $GLOBALS['_DATE_SPAN_FORMAT'] = $format;
912 * Get the default format.
914 * @return mixed Default format.
918 function getDefaultFormat()
920 return $GLOBALS['_DATE_SPAN_FORMAT'];
924 * Returns a copy of the object (workarround for PHP5 forward compatibility).
926 * @return object Date_Span Copy of the object.
929 $c = get_class($this);
931 $s->day = $this->day;
932 $s->hour = $this->hour;
933 $s->minute = $this->minute;
934 $s->second = $this->second;