or($min / 60); $day = intval(floor($hour / 24)); $this->second = $sec % 60; $this->minute = $min % 60; $this->hour = $hour % 24; $this->day = $day; return true; } // }}} // {{{ setFromMinutes() /** * Set the time span from a total number of minutes. * * @param float $minutes Total number of minutes. * * @return bool True on success. * * @access public */ function setFromMinutes($minutes) { return $this->setFromSeconds(round($minutes * 60)); } // }}} // {{{ setFromHours() /** * Set the time span from a total number of hours. * * @param float $hours Total number of hours. * * @return bool True on success. * * @access public */ function setFromHours($hours) { return $this->setFromSeconds(round($hours * 3600)); } // }}} // {{{ setFromDays() /** * Set the time span from a total number of days. * * @param float $days Total number of days. * * @return bool True on success. * * @access public */ function setFromDays($days) { return $this->setFromSeconds(round($days * 86400)); } // }}} // {{{ setFromDateDiff() /** * Set the span from the elapsed time between two dates. * * Set the span from the elapsed time between two dates. The time span * is allways positive, so the date's order is not important. * * @param object Date $date1 First Date. * @param object Date $date2 Second Date. * * @return bool True on success. * * @access public */ function setFromDateDiff($date1, $date2) { if (!is_a($date1, 'date') or !is_a($date2, 'date')) { return false; } $date1->toUTC(); $date2->toUTC(); if ($date1->after($date2)) { list($date1, $date2) = array($date2, $date1); } $days = Date_Calc::dateDiff( $date1->getDay(), $date1->getMonth(), $date1->getYear(), $date2->getDay(), $date2->getMonth(), $date2->getYear() ); $hours = $date2->getHour() - $date1->getHour(); $mins = $date2->getMinute() - $date1->getMinute(); $secs = $date2->getSecond() - $date1->getSecond(); $this->setFromSeconds( $days * 86400 + $hours * 3600 + $mins * 60 + $secs ); return true; } // }}} // {{{ copy() /** * Set the time span from another time object. * * @param object Date_Span $time Source time span object. * * @return bool True on success. * * @access public */ function copy($time) { if (is_a($time, 'date_span')) { $this->second = $time->second; $this->minute = $time->minute; $this->hour = $time->hour; $this->day = $time->day; return true; } else { return false; } } // }}} // {{{ format() /** * Time span pretty printing (similar to Date::format()). * * Formats the time span in the given format, similar to * strftime() and Date::format().
*
* Formatting 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).
* %E Total hours as a decimal number * (1 day, 2 hours, 40 minutes = 26 hours).
* %f Total minutes as a float number * (2 minutes, 30 seconds = 2.5 minutes).
* %F Total minutes as a decimal number * (1 hour, 2 minutes, 40 seconds = 62 minutes).
* %g Total seconds as a decimal number * (2 minutes, 30 seconds = 90 seconds).
* %h Hours as decimal number (0 to 23).
* %H Hours as decimal number (00 to 23).
* %i Hours as decimal number on 12-hour clock * (1 to 12).
* %I Hours as decimal number on 12-hour clock * (01 to 12).
* %m Minutes as a decimal number (0 to 59).
* %M Minutes as a decimal number (00 to 59).
* %n Newline character (\n).
* %p Either 'am' or 'pm' depending on the time.
* %P Either 'AM' or 'PM' depending on the time.
* %r Time in am/pm notation, same as "%I:%M:%S %p".
* %R Time in 24-hour notation, same as "%H:%M".
* %s Seconds as a decimal number (0 to 59).
* %S Seconds as a decimal number (00 to 59).
* %t Tab character (\t).
* %T Current time equivalent, same as "%H:%M:%S".
* %% Literal '%'.
* * @param string $format The format string for returned time span. * * @return string The time span in specified format. * * @access public */ function format($format = null) { if (is_null($format)) { $format = $GLOBALS['_DATE_SPAN_FORMAT']; } $output = ''; for ($i = 0; $i < strlen($format); $i++) { $char = $format{$i}; if ($char == '%') { $nextchar = $format{++$i}; switch ($nextchar) { case 'C': $output .= sprintf( '%d, %02d:%02d:%02d', $this->day, $this->hour, $this->minute, $this->second ); break; case 'd': $output .= $this->toDays(); break; case 'D': $output .= $this->day; break; case 'e': $output .= $this->toHours(); break; case 'E': $output .= floor($this->toHours()); break; case 'f': $output .= $this->toMinutes(); break; case 'F': $output .= floor($this->toMinutes()); break; case 'g': $output .= $this->toSeconds(); break; case 'h': $output .= $this->hour; break; case 'H': $output .= sprintf('%02d', $this->hour); break; case 'i': $hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour; $output .= ($hour == 0) ? 12 : $hour; break; case 'I': $hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour; $output .= sprintf('%02d', $hour==0 ? 12 : $hour); break; case 'm': $output .= $this->minute; break; case 'M': $output .= sprintf('%02d',$this->minute); break; case 'n': $output .= "\n"; break; case 'p': $output .= $this->hour >= 12 ? 'pm' : 'am'; break; case 'P': $output .= $this->hour >= 12 ? 'PM' : 'AM'; break; case 'r': $hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour; $output .= sprintf( '%02d:%02d:%02d %s', $hour==0 ? 12 : $hour, $this->minute, $this->second, $this->hour >= 12 ? 'pm' : 'am' ); break; case 'R': $output .= sprintf( '%02d:%02d', $this->hour, $this->minute ); break; case 's': $output .= $this->second; break; case 'S': $output .= sprintf('%02d', $this->second); break; case 't': $output .= "\t"; break; case 'T': $output .= sprintf( '%02d:%02d:%02d', $this->hour, $this->minute, $this->second ); break; case '%': $output .= "%"; break; default: $output .= $char . $nextchar; } } else { $output .= $char; } } return $output; } // }}} // {{{ toSeconds() /** * Convert time span to seconds. * * @return int Time span as an integer number of seconds. * * @access public */ function toSeconds() { return $this->day * 86400 + $this->hour * 3600 + $this->minute * 60 + $this->second; } // }}} // {{{ toMinutes() /** * Convert time span to minutes. * * @return float Time span as a decimal number of minutes. * * @access public */ function toMinutes() { return $this->day * 1440 + $this->hour * 60 + $this->minute + $this->second / 60; } // }}} // {{{ toHours() /** * Convert time span to hours. * * @return float Time span as a decimal number of hours. * * @access public */ function toHours() { return $this->day * 24 + $this->hour + $this->minute / 60 + $this->second / 3600; } // }}} // {{{ toDays() /** * Convert time span to days. * * @return float Time span as a decimal number of days. * * @access public */ function toDays() { return $this->day + $this->hour / 24 + $this->minute / 1440 + $this->second / 86400; } // }}} // {{{ add() /** * Adds a time span. * * @param object Date_Span $time Time span to add. * * @access public */ function add($time) { return $this->setFromSeconds( $this->toSeconds() + $time->toSeconds() ); } // }}} // {{{ substract() /** * Subtracts a time span. * * Subtracts a time span. If the time span to subtract is large