* @param mixed $format Format string to set it from a string or the * second date set it from a date diff. * * @see set() * @access public */ function Date_Span($time = 0, $format = null) { $this->set($time, $format); } // }}} // {{{ set() /** * Set the time span to a new value in a 'smart' way. * * Sets the time span depending on the argument types, calling * to the appropriate setFromXxx() method. * * @param mixed $time Time span expression. * @param mixed $format Format string to set it from a string or the * second date set it from a date diff. * * @return bool true on success. * * @see setFromObject() * @see setFromArray() * @see setFromString() * @see setFromSeconds() * @see setFromDateDiff() * @access public */ function set($time = 0, $format = null) { if (is_a($time, 'date_span')) { return $this->copy($time); } elseif (is_a($time, 'date') and is_a($format, 'date')) { return $this->setFromDateDiff($time, $format); } elseif (is_array($time)) { return $this->setFromArray($time); } elseif (is_string($time)) { return $this->setFromString($time, $format); } elseif (is_int($time)) { return $this->setFromSeconds($time); } else { return $this->setFromSeconds(0); } } // }}} // {{{ setFromArray() /** * Set the time span from an array. * * Set the time span from an array. Any value can be a float (but it * has no sense in seconds), for example array(23.5, 20, 0) is * interpreted as 23 hours, .5*60 + 20 = 50 minutes and 0 seconds. * * @param array $time Items are counted from right to left. First * item is for seconds, second for minutes, third * for hours and fourth for days. If there are * less items than 4, zero (0) is assumed for the * absent values. * * @return bool True on success. * * @access public */ function setFromArray($time) { if (!is_array($time)) { return false; } $tmp1 = new Date_Span; if (!$tmp1->setFromSeconds(@array_pop($time))) { return false; } $tmp2 = new Date_Span; if (!$tmp2->setFromMinutes(@array_pop($time))) { return false; } $tmp1->add($tmp2); if (!$tmp2->setFromHours(@array_pop($time))) { return false; } $tmp1->add($tmp2); if (!$tmp2->setFromDays(@array_pop($time))) { return false; } $tmp1->add($tmp2); return $this->copy($tmp1); } // }}} // {{{ setFromString() /** * Set the time span from a string based on an input format. * * Set the time span from a string based on an input format. This is * some like a mix of format() method and sscanf() PHP function. The * error checking and validation of this function is very primitive, * so you should be carefull when using it with unknown $time strings. * With this method you are assigning day, hour, minute and second * values, and the last values are used. This means that if you use * something like setFromString('10, 20', '%H, %h') your time span * would be 20 hours long. Allways remember that this method set * all the values, so if you had a $time span 30 minutes long * and you make $time->setFromString('20 hours', '%H hours'), $time * span would be 20 hours long (and not 20 hours and 30 minutes). * Input format options:
* %C Days with time, same as "%D, %H:%M:%S".
* %d Total days as a float number * (2 days, 12 hours = 2.5 days).
* %D Days as a decimal number.
* %e Total hours as a float number * (1 day, 2 hours, 30 minutes = 26.5 hours).
* %f Total minutes as a float number * (2 minutes, 30 seconds = 2.5 minutes).
* %g Total seconds as a decimal number * (2 minutes, 30 seconds = 90 seconds).
* %h Hours as decimal number.
* %H Hours as decimal number limited to 2 digits.
* %m Minutes as a decimal number.
* %M Minutes as a decimal number limited to 2 digits.
* %n Newline character (\n).
* %p Either 'am' or 'pm' depending on the time. If 'pm' * is detected it adds 12 hours to the resulting time * span (without any checks). This is case * insensitive.
* %r Time in am/pm notation, same as "%H:%M:%S %p".
* %R Time in 24-hour notation, same as "%H:%M".
* %s Seconds as a decimal number.
* %S Seconds as a decimal number limited to 2 digits.
* %t Tab character (\t).
* %T Current time equivalent, same as "%H:%M:%S".
* %% Literal '%'.
* * @param string $time String from where to get the time span * information. * @param string $format Format string. * * @return bool True on success. * * @access public */ function setFromString($time, $format = null) { if (is_null($format)) { $format = $GLOBALS['_DATE_SPAN_INPUT_FORMAT']; } // If format is a string, it parses the string format. if (is_string($format)) { $str = ''; $vars = array(); $pm = 'am'; $day = $hour = $minute = $second = 0; for ($i = 0; $i < strlen($format); $i++) { $char = $format{$i}; if ($char == '%') { $nextchar = $format{++$i}; switch ($nextchar) { case 'c': $str .= '%d, %d:%d:%d'; array_push( $vars, 'day', 'hour', 'minute', 'second'); break; case 'C': $str .= '%d, %2d:%2d:%2d'; array_push( $vars, 'day', 'hour', 'minute', 'second'); break; case 'd': $str .= '%f'; array_push($vars, 'day'); break; case 'D': $str .= '%d'; array_push($vars, 'day'); break; case 'e': $str .= '%f'; array_push($vars, 'hour'); break; case 'f': $str .= '%f'; array_push($vars, 'minute'); break; case 'g': $str .= '%f'; array_push($vars, 'second'); break; case 'h': $str .= '%d'; array_push($vars, 'hour'); break; case 'H': $str .= '%2d'; array_push($vars, 'hour'); break; case 'm': $str .= '%d'; array_push($vars, 'minute'); break; case 'M': $str .= '%2d'; array_push($vars, 'minute'); break; case 'n': $str .= "\n"; break; case 'p': $str .= '%2s'; array_push($vars, 'pm'); break; case 'r': $str .= '%2d:%2d:%2d %2s'; array_push( $vars, 'hour', 'minute', 'second', 'pm'); break; case 'R': $str .= '%2d:%2d'; array_push($vars, 'hour', 'minute'); break; case 's': $str .= '%d'; array_push($vars, 'second'); break; case 'S': $str .= '%2d'; array_push($vars, 'second'); break; case 't': $str .= "\t"; break; case 'T': $str .= '%2d:%2d:%2d'; array_push($vars, 'hour', 'minute', 'second'); break; case '%': $str .= "%"; break; default: $str .= $char . $nextchar; } } else { $str .= $char; } } $vals = sscanf($time, $str); foreach ($vals as $i => $val) { if (is_null($val)) { return false; } $$vars[$i] = $val; } if (strcasecmp($pm, 'pm') == 0) { $hour += 12; } elseif (strcasecmp($pm, 'am') != 0) { return false; } $this->setFromArray(array($day, $hour, $minute, $second)); // If format is a integer, it uses a pred