Date
[ class tree: Date ] [ index: Date ] [ all elements ]

Source for file Span.php

Documentation is available at Span.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3.  
  4. // {{{ Header
  5.  
  6. /**
  7.  * Generic time span handling class for PEAR
  8.  *
  9.  * PHP versions 4 and 5
  10.  *
  11.  * LICENSE:
  12.  *
  13.  * Copyright (c) 1997-2005 Leandro Lucarella, Pierre-Alain Joye
  14.  * All rights reserved.
  15.  *
  16.  * Redistribution and use in source and binary forms, with or without
  17.  * modification, are permitted under the terms of the BSD License.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * @category   Date and Time
  33.  * @package    Date
  34.  * @author     Leandro Lucarella <llucax@php.net>
  35.  * @author     Pierre-Alain Joye <pajoye@php.net>
  36.  * @copyright  1997-2006 Leandro Lucarella, Pierre-Alain Joye
  37.  * @license    http://www.opensource.org/licenses/bsd-license.php
  38.  *              BSD License
  39.  * @version    CVS: $Id: Span.php,v 1.15 2007/11/29 19:26:08 c01234 Exp $
  40.  * @link       http://pear.php.net/package/Date
  41.  * @since      File available since Release 1.4
  42.  */
  43.  
  44. // }}}
  45. // {{{ Includes
  46.  
  47. /**
  48.  * Get the Date class
  49.  */
  50. require_once 'Date.php';
  51.  
  52. /**
  53.  * Get the Date_Calc class
  54.  */
  55. require_once 'Date/Calc.php';
  56.  
  57. // }}}
  58. // {{{ Constants
  59.  
  60. /**
  61.  * Non Numeric Separated Values (NNSV) Input Format
  62.  *
  63.  * Input format guessed from something like this:
  64.  * days<sep>hours<sep>minutes<sep>seconds
  65.  * Where <sep> is any quantity of non numeric chars. If no values are
  66.  * given, time span is set to zero, if one value is given, it's used for
  67.  * hours, if two values are given it's used for hours and minutes and if
  68.  * three values are given, it's used for hours, minutes and seconds.<br>
  69.  * Examples:<br>
  70.  * ''                   -> 0, 0, 0, 0 (days, hours, minutes, seconds)<br>
  71.  * '12'                 -> 0, 12, 0, 0
  72.  * '12.30'              -> 0, 12, 30, 0<br>
  73.  * '12:30:18'           -> 0, 12, 30, 18<br>
  74.  * '3-12-30-18'         -> 3, 12, 30, 18<br>
  75.  * '3 days, 12-30-18'   -> 3, 12, 30, 18<br>
  76.  * '12:30 with 18 secs' -> 0, 12, 30, 18<br>
  77.  *
  78.  * @const int
  79.  */
  80. define('DATE_SPAN_INPUT_FORMAT_NNSV'1);
  81.  
  82. // }}}
  83. // {{{ Global Variables
  84.  
  85. /**
  86.  * Default time format when converting to a string
  87.  *
  88.  * @global string 
  89.  */
  90. $GLOBALS['_DATE_SPAN_FORMAT''%C';
  91.  
  92. /**
  93.  * Default time format when converting from a string
  94.  *
  95.  * @global mixed 
  96.  */
  97. $GLOBALS['_DATE_SPAN_INPUT_FORMAT'DATE_SPAN_INPUT_FORMAT_NNSV;
  98.  
  99. // }}}
  100. // {{{ Class: Date_Span
  101.  
  102. /**
  103.  * Generic time span handling class for PEAR
  104.  *
  105.  * @category  Date and Time
  106.  * @package   Date
  107.  * @author    Leandro Lucarella <llucax@php.net>
  108.  * @author    Pierre-Alain Joye <pajoye@php.net>
  109.  * @copyright 1997-2006 Leandro Lucarella, Pierre-Alain Joye
  110.  * @license   http://www.opensource.org/licenses/bsd-license.php
  111.  *             BSD License
  112.  * @version   Release: 1.5.0a1
  113.  * @link      http://pear.php.net/package/Date
  114.  * @since     Class available since Release 1.4
  115.  */
  116. class Date_Span
  117. {
  118.  
  119.     // {{{ Properties
  120.  
  121.     /**
  122.      * The no of days
  123.      *
  124.      * @var      int 
  125.      * @access   private
  126.      * @since    Property available since Release 1.0
  127.      */
  128.     var $day;
  129.  
  130.     /**
  131.      * The no of hours (0 to 23)
  132.      *
  133.      * @var      int 
  134.      * @access   private
  135.      * @since    Property available since Release 1.0
  136.      */
  137.     var $hour;
  138.  
  139.     /**
  140.      * The no of minutes (0 to 59)
  141.      *
  142.      * @var      int 
  143.      * @access   private
  144.      * @since    Property available since Release 1.0
  145.      */
  146.     var $minute;
  147.  
  148.     /**
  149.      * The no of seconds (0 to 59)
  150.      *
  151.      * @var      int 
  152.      * @access   private
  153.      * @since    Property available since Release 1.0
  154.      */
  155.     var $second;
  156.  
  157.  
  158.     // }}}
  159.     // {{{ Constructor
  160.  
  161.     /**
  162.      * Constructor
  163.      *
  164.      * Creates the time span object calling the set() method.
  165.      *
  166.      * @param mixed $time   time span expression
  167.      * @param mixed $format format string to set it from a string or the
  168.      *                        second date set it from a date diff
  169.      *
  170.      * @access   public
  171.      * @see      set()
  172.      */
  173.     function Date_Span($time = 0$format = null)
  174.     {
  175.         $this->set($time$format);
  176.     }
  177.  
  178.  
  179.     // }}}
  180.     // {{{ set()
  181.  
  182.     /**
  183.      * Set the time span to a new value in a 'smart' way
  184.      *
  185.      * Sets the time span depending on the argument types, calling
  186.      * to the appropriate setFromXxx() method.
  187.      *
  188.      * @param mixed $time   time span expression
  189.      * @param mixed $format format string to set it from a string or the
  190.      *                        second date set it from a date diff
  191.      *
  192.      * @return   bool       true on success
  193.      * @access   public
  194.      * @see      setFromObject(), setFromArray(), setFromString(),
  195.      *             setFromSeconds(), setFromDateDiff()
  196.      */
  197.     function set($time = 0$format = null)
  198.     {
  199.         if (is_a($time'date_span')) {
  200.             return $this->copy($time);
  201.         elseif (is_a($time'date'and is_a($format'date')) {
  202.             return $this->setFromDateDiff($time$format);
  203.         elseif (is_array($time)) {
  204.             return $this->setFromArray($time);
  205.         elseif (is_string($time)) {
  206.             return $this->setFromString($time$format);
  207.         elseif (is_int($time)) {
  208.             return $this->setFromSeconds($time);
  209.         else {
  210.             return $this->setFromSeconds(0);
  211.         }
  212.     }
  213.  
  214.  
  215.     // }}}
  216.     // {{{ setFromArray()
  217.  
  218.     /**
  219.      * Set the time span from an array
  220.      *
  221.      * Any value can be a float (but it has no sense in seconds), for example:
  222.      *
  223.      *  <code>array(23.5, 20, 0)</code>
  224.      *
  225.      * is interpreted as 23 hours, .5*60 + 20 = 50 minutes and 0 seconds.
  226.      *
  227.      * @param array $time items are counted from right to left. First
  228.      *                      item is for seconds, second for minutes, third
  229.      *                      for hours and fourth for days. If there are
  230.      *                      less items than 4, zero (0) is assumed for the
  231.      *                      absent values.
  232.      *
  233.      * @return   bool       true on success
  234.      * @access   public
  235.      */
  236.     function setFromArray($time)
  237.     {
  238.         if (!is_array($time)) {
  239.             return false;
  240.         }
  241.         $tmp1 = new Date_Span;
  242.         if (!$tmp1->setFromSeconds(@array_pop($time))) {
  243.             return false;
  244.         }
  245.         $tmp2 = new Date_Span;
  246.         if (!$tmp2->setFromMinutes(@array_pop($time))) {
  247.             return false;
  248.         }
  249.         $tmp1->add($tmp2);
  250.         if (!$tmp2->setFromHours(@array_pop($time))) {
  251.             return false;
  252.         }
  253.         $tmp1->add($tmp2);
  254.         if (!$tmp2->setFromDays(@array_pop($time))) {
  255.             return false;
  256.         }
  257.         $tmp1->add($tmp2);
  258.         return $this->copy($tmp1);
  259.     }
  260.  
  261.  
  262.     // }}}
  263.     // {{{ setFromString()
  264.  
  265.     /**
  266.      * Set the time span from a string based on an input format
  267.      *
  268.      * This is some like a mix of format() method and sscanf() PHP function.
  269.      * The error checking and validation of this function is very primitive,
  270.      * so you should be carefull when using it with unknown $time strings.
  271.      * With this method you are assigning day, hour, minute and second
  272.      * values, and the last values are used. This means that if you use
  273.      * something like setFromString('10, 20', '%H, %h') your time span
  274.      * would be 20 hours long. Allways remember that this method sets
  275.      * <b>all</b> the values, so if you had a $time span 30 minutes long
  276.      * and you make $time->setFromString('20 hours', '%H hours'), $time
  277.      * span would be 20 hours long (and not 20 hours and 30 minutes).
  278.      * Input format options:<br>
  279.      *  <code>%C</code> Days with time, same as "%D, %H:%M:%S"<br>
  280.      *  <code>%d</code> Total days as a float number
  281.      *                  (2 days, 12 hours = 2.5 days)<br>
  282.      *  <code>%D</code> Days as a decimal number<br>
  283.      *  <code>%e</code> Total hours as a float number
  284.      *                  (1 day, 2 hours, 30 minutes = 26.5 hours)<br>
  285.      *  <code>%f</code> Total minutes as a float number
  286.      *                  (2 minutes, 30 seconds = 2.5 minutes)<br>
  287.      *  <code>%g</code> Total seconds as a decimal number
  288.      *                  (2 minutes, 30 seconds = 90 seconds)<br>
  289.      *  <code>%h</code> Hours as decimal number<br>
  290.      *  <code>%H</code> Hours as decimal number limited to 2 digits<br>
  291.      *  <code>%m</code> Minutes as a decimal number<br>
  292.      *  <code>%M</code> Minutes as a decimal number limited to 2 digits<br>
  293.      *  <code>%n</code> Newline character (\n)<br>
  294.      *  <code>%p</code> Either 'am' or 'pm' depending on the time. If 'pm'
  295.      *                  is detected it adds 12 hours to the resulting time
  296.      *                  span (without any checks). This is case
  297.      *                  insensitive.<br>
  298.      *  <code>%r</code> Time in am/pm notation, same as "%H:%M:%S %p"<br>
  299.      *  <code>%R</code> Time in 24-hour notation, same as "%H:%M"<br>
  300.      *  <code>%s</code> Seconds as a decimal number<br>
  301.      *  <code>%S</code> Seconds as a decimal number limited to 2 digits<br>
  302.      *  <code>%t</code> Tab character (\t)<br>
  303.      *  <code>%T</code> Current time equivalent, same as "%H:%M:%S"<br>
  304.      *  <code>%%</code> Literal '%'<br>
  305.      *
  306.      * @param string $time   string from where to get the time span
  307.      *                         information
  308.      * @param string $format format string
  309.      *
  310.      * @return   bool       true on success
  311.      * @access   public
  312.      */
  313.     function setFromString($time$format = null)
  314.     {
  315.         if (is_null($format)) {
  316.             $format $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
  317.         }
  318.         // If format is a string, it parses the string format.
  319.         if (is_string($format)) {
  320.             $str  '';
  321.             $vars = array();
  322.             $pm   'am';
  323.             $day  $hour $minute $second = 0;
  324.             for ($i = 0; $i strlen($format)$i++{
  325.                 $char $format{$i};
  326.                 if ($char == '%'{
  327.                     $nextchar $format{++$i};
  328.                     switch ($nextchar{
  329.                     case 'c':
  330.                         $str .= '%d, %d:%d:%d';
  331.                         array_push($vars,
  332.                                    'day',
  333.                                    'hour',
  334.                                    'minute',
  335.                                    'second');
  336.                         break;
  337.                     case 'C':
  338.                         $str .= '%d, %2d:%2d:%2d';
  339.                         array_push($vars,
  340.                                    'day',
  341.                                    'hour',
  342.                                    'minute',
  343.                                    'second');
  344.                         break;
  345.                     case 'd':
  346.                         $str .= '%f';
  347.                         array_push($vars'day');
  348.                         break;
  349.                     case 'D':
  350.                         $str .= '%d';
  351.                         array_push($vars'day');
  352.                         break;
  353.                     case 'e':
  354.                         $str .= '%f';
  355.                         array_push($vars'hour');
  356.                         break;
  357.                     case 'f':
  358.                         $str .= '%f';
  359.                         array_push($vars'minute');
  360.                         break;
  361.                     case 'g':
  362.                         $str .= '%f';
  363.                         array_push($vars'second');
  364.                         break;
  365.                     case 'h':
  366.                         $str .= '%d';
  367.                         array_push($vars'hour');
  368.                         break;
  369.                     case 'H':
  370.                         $str .= '%2d';
  371.                         array_push($vars'hour');
  372.                         break;
  373.                     case 'm':
  374.                         $str .= '%d';
  375.                         array_push($vars'minute');
  376.                         break;
  377.                     case 'M':
  378.                         $str .= '%2d';
  379.                         array_push($vars'minute');
  380.                         break;
  381.                     case 'n':
  382.                         $str .= "\n";
  383.                         break;
  384.                     case 'p':
  385.                         $str .= '%2s';
  386.                         array_push($vars'pm');
  387.                         break;
  388.                     case 'r':
  389.                         $str .= '%2d:%2d:%2d %2s';
  390.                         array_push($vars,
  391.                                    'hour',
  392.                                    'minute',
  393.                                    'second',
  394.                                    'pm');
  395.                         break;
  396.                     case 'R':
  397.                         $str .= '%2d:%2d';
  398.                         array_push($vars'hour''minute');
  399.                         break;
  400.                     case 's':
  401.                         $str .= '%d';
  402.                         array_push($vars'second');
  403.                         break;
  404.                     case 'S':
  405.                         $str .= '%2d';
  406.                         array_push($vars'second');
  407.                         break;
  408.                     case 't':
  409.                         $str .= "\t";
  410.                         break;
  411.                     case 'T':
  412.                         $str .= '%2d:%2d:%2d';
  413.                         array_push($vars'hour''minute''second');
  414.                         break;
  415.                     case '%':
  416.                         $str .= "%";
  417.                         break;
  418.                     default:
  419.                         $str .= $char $nextchar;
  420.                     }
  421.                 else {
  422.                     $str .= $char;
  423.                 }
  424.             }
  425.             $vals sscanf($time$str);
  426.             foreach ($vals as $i => $val{
  427.                 if (is_null($val)) {
  428.                     return false;
  429.                 }
  430.                 $$vars[$i$val;
  431.             }
  432.             if (strcasecmp($pm'pm'== 0{
  433.                 $hour += 12;
  434.             elseif (strcasecmp($pm'am'!= 0{
  435.                 return false;
  436.             }
  437.             $this->setFromArray(array($day$hour$minute$second));
  438.         elseif (is_integer($format)) {
  439.             // If format is a integer, it uses a predefined format
  440.             // detection method.
  441.             switch ($format{
  442.             case DATE_SPAN_INPUT_FORMAT_NNSV:
  443.                 $time preg_split('/\D+/'$time);
  444.                 switch (count($time)) {
  445.                 case 0:
  446.                     return $this->setFromArray(array(0,
  447.                                                      0,
  448.                                                      0,
  449.                                                      0));
  450.                 case 1:
  451.                     return $this->setFromArray(array(0,
  452.                                                      $time[0],
  453.                                                      0,
  454.                                                      0));
  455.                 case 2:
  456.                     return $this->setFromArray(array(0,
  457.                                                      $time[0],
  458.                                                      $time[1],
  459.                                                      0));
  460.                 case 3:
  461.                     return $this->setFromArray(array(0,
  462.                                                      $time[0],
  463.                                                      $time[1],
  464.                                                      $time[2]));
  465.                 default:
  466.                     return $this->setFromArray($time);
  467.                 }
  468.                 break;
  469.             }
  470.         }
  471.         return false;
  472.     }
  473.  
  474.  
  475.     // }}}
  476.     // {{{ setFromSeconds()
  477.  
  478.     /**
  479.      * Set the time span from a total number of seconds
  480.      *
  481.      * @param int $seconds total number of seconds
  482.      *
  483.      * @return   bool       true on success
  484.      * @access   public
  485.      */
  486.     function setFromSeconds($seconds)
  487.     {
  488.         if ($seconds < 0{
  489.             return false;
  490.         }
  491.         $sec  intval($seconds);
  492.         $min  floor($sec / 60);
  493.         $hour floor($min / 60);
  494.         $day  intval(floor($hour / 24));
  495.  
  496.         $this->second $sec % 60;
  497.         $this->minute $min % 60;
  498.         $this->hour   $hour % 24;
  499.         $this->day    $day;
  500.         return true;
  501.     }
  502.  
  503.  
  504.     // }}}
  505.     // {{{ setFromMinutes()
  506.  
  507.     /**
  508.      * Set the time span from a total number of minutes
  509.      *
  510.      * @param float $minutes total number of minutes
  511.      *
  512.      * @return   bool       true on success
  513.      * @access   public
  514.      */
  515.     function setFromMinutes($minutes)
  516.     {
  517.         return $this->setFromSeconds(round($minutes * 60));
  518.     }
  519.  
  520.  
  521.     // }}}
  522.     // {{{ setFromHours()
  523.  
  524.     /**
  525.      * Set the time span from a total number of hours
  526.      *
  527.      * @param float $hours total number of hours
  528.      *
  529.      * @return   bool       true on success
  530.      * @access   public
  531.      */
  532.     function setFromHours($hours)
  533.     {
  534.         return $this->setFromSeconds(round($hours * 3600));
  535.     }
  536.  
  537.  
  538.     // }}}
  539.     // {{{ setFromDays()
  540.  
  541.     /**
  542.      * Set the time span from a total number of days
  543.      *
  544.      * @param float $days total number of days
  545.      *
  546.      * @return   bool       true on success
  547.      * @access   public
  548.      */
  549.     function setFromDays($days)
  550.     {
  551.         return $this->setFromSeconds(round($days * 86400));
  552.     }
  553.  
  554.  
  555.     // }}}
  556.     // {{{ setFromDateDiff()
  557.  
  558.     /**
  559.      * Set the span from the elapsed time between two dates
  560.      *
  561.      * The time span is unsigned, so the date's order is not important.
  562.      *
  563.      * @param object $date1 first Date
  564.      * @param object $date2 second Date
  565.      *
  566.      * @return   bool       true on success
  567.      * @access   public
  568.      */
  569.     function setFromDateDiff($date1$date2)
  570.     {
  571.         if (!is_a($date1'date'or !is_a($date2'date')) {
  572.             return false;
  573.         }
  574.         $date1->toUTC();
  575.         $date2->toUTC();
  576.         if ($date1->after($date2)) {
  577.             list($date1$date2= array($date2$date1);
  578.         }
  579.         $days  Date_Calc::dateDiff($date1->getDay(),
  580.                                      $date1->getMonth(),
  581.                                      $date1->getYear(),
  582.                                      $date2->getDay(),
  583.                                      $date2->getMonth(),
  584.                                      $date2->getYear());
  585.         $hours $date2->getHour($date1->getHour();
  586.         $mins  $date2->getMinute($date1->getMinute();
  587.         $secs  $date2->getSecond($date1->getSecond();
  588.         $this->setFromSeconds($days * 86400 +
  589.                               $hours * 3600 +
  590.                               $mins * 60 + $secs);
  591.         return true;
  592.     }
  593.  
  594.  
  595.     // }}}
  596.     // {{{ copy()
  597.  
  598.     /**
  599.      * Set the time span from another time object
  600.      *
  601.      * @param object $time source time span object
  602.      *
  603.      * @return   bool       true on success
  604.      * @access   public
  605.      */
  606.     function copy($time)
  607.     {
  608.         if (is_a($time'date_span')) {
  609.             $this->second $time->second;
  610.             $this->minute $time->minute;
  611.             $this->hour   $time->hour;
  612.             $this->day    $time->day;
  613.             return true;
  614.         else {
  615.             return false;
  616.         }
  617.     }
  618.  
  619.  
  620.     // }}}
  621.     // {{{ format()
  622.  
  623.     /**
  624.      * Time span pretty printing (similar to Date::format())
  625.      *
  626.      * Formats the time span in the given format, similar to
  627.      * strftime() and Date::format().<br>
  628.      * <br>
  629.      * Formatting options:<br>
  630.      *  <code>%C</code> Days with time, same as "%D, %H:%M:%S"<br>
  631.      *  <code>%d</code> Total days as a float number
  632.      *                  (2 days, 12 hours = 2.5 days)<br>
  633.      *  <code>%D</code> Days as a decimal number<br>
  634.      *  <code>%e</code> Total hours as a float number
  635.      *                  (1 day, 2 hours, 30 minutes = 26.5 hours)<br>
  636.      *  <code>%E</code> Total hours as a decimal number
  637.      *                  (1 day, 2 hours, 40 minutes = 26 hours)<br>
  638.      *  <code>%f</code> Total minutes as a float number
  639.      *                  (2 minutes, 30 seconds = 2.5 minutes)<br>
  640.      *  <code>%F</code> Total minutes as a decimal number
  641.      *                  (1 hour, 2 minutes, 40 seconds = 62 minutes)<br>
  642.      *  <code>%g</code> Total seconds as a decimal number
  643.      *                  (2 minutes, 30 seconds = 90 seconds)<br>
  644.      *  <code>%h</code> Hours as decimal number (0 to 23)<br>
  645.      *  <code>%H</code> Hours as decimal number (00 to 23)<br>
  646.      *  <code>%i</code> Hours as decimal number on 12-hour clock
  647.      *                  (1 to 12)<br>
  648.      *  <code>%I</code> Hours as decimal number on 12-hour clock
  649.      *                  (01 to 12)<br>
  650.      *  <code>%m</code> Minutes as a decimal number (0 to 59)<br>
  651.      *  <code>%M</code> Minutes as a decimal number (00 to 59)<br>
  652.      *  <code>%n</code> Newline character (\n)<br>
  653.      *  <code>%p</code> Either 'am' or 'pm' depending on the time<br>
  654.      *  <code>%P</code> Either 'AM' or 'PM' depending on the time<br>
  655.      *  <code>%r</code> Time in am/pm notation, same as "%I:%M:%S %p"<br>
  656.      *  <code>%R</code> Time in 24-hour notation, same as "%H:%M"<br>
  657.      *  <code>%s</code> Seconds as a decimal number (0 to 59)<br>
  658.      *  <code>%S</code> Seconds as a decimal number (00 to 59)<br>
  659.      *  <code>%t</code> Tab character (\t)<br>
  660.      *  <code>%T</code> Current time equivalent, same as "%H:%M:%S"<br>
  661.      *  <code>%%</code> Literal '%'<br>
  662.      *
  663.      * @param string $format the format string for returned time span
  664.      *
  665.      * @return   string     the time span in specified format
  666.      * @access   public
  667.      */
  668.     function format($format = null)
  669.     {
  670.         if (is_null($format)) {
  671.             $format $GLOBALS['_DATE_SPAN_FORMAT'];
  672.         }
  673.         $output '';
  674.         for ($i = 0; $i strlen($format)$i++{
  675.             $char $format{$i};
  676.             if ($char == '%'{
  677.                 $nextchar $format{++$i};
  678.                 switch ($nextchar{
  679.                 case 'C':
  680.                     $output .= sprintf('%d, %02d:%02d:%02d',
  681.                                       $this->day,
  682.                                       $this->hour,
  683.                                       $this->minute,
  684.                                       $this->second);
  685.                     break;
  686.                 case 'd':
  687.                     $output .= $this->toDays();
  688.                     break;
  689.                 case 'D':
  690.                     $output .= $this->day;
  691.                     break;
  692.                 case 'e':
  693.                     $output .= $this->toHours();
  694.                     break;
  695.                 case 'E':
  696.                     $output .= floor($this->toHours());
  697.                     break;
  698.                 case 'f':
  699.                     $output .= $this->toMinutes();
  700.                     break;
  701.                 case 'F':
  702.                     $output .= floor($this->toMinutes());
  703.                     break;
  704.                 case 'g':
  705.                     $output .= $this->toSeconds();
  706.                     break;
  707.                 case 'h':
  708.                     $output .= $this->hour;
  709.                     break;
  710.                 case 'H':
  711.                     $output .= sprintf('%02d'$this->hour);
  712.                     break;
  713.                 case 'i':
  714.                 case 'I':
  715.                     $hour    $this->hour + 1 > 12 ?
  716.                                $this->hour - 12 :
  717.                                $this->hour;
  718.                     $output .= $hour == 0 ?
  719.                                12 :
  720.                                ($nextchar == "i" ?
  721.                                 $hour :
  722.                                 sprintf('%02d'$hour));
  723.                     break;
  724.                 case 'm':
  725.                     $output .= $this->minute;
  726.                     break;
  727.                 case 'M':
  728.                     $output .= sprintf('%02d'$this->minute);
  729.                     break;
  730.                 case 'n':
  731.                     $output .= "\n";
  732.                     break;
  733.                 case 'p':
  734.                     $output .= $this->hour >= 12 ? 'pm' 'am';
  735.                     break;
  736.                 case 'P':
  737.                     $output .= $this->hour >= 12 ? 'PM' 'AM';
  738.                     break;
  739.                 case 'r':
  740.                     $hour    $this->hour + 1 > 12 ?
  741.                                $this->hour - 12 :
  742.                                $this->hour;
  743.                     $output .= sprintf('%02d:%02d:%02d %s',
  744.                                        $hour == 0 ?  12 : $hour,
  745.                                        $this->minute,
  746.                                        $this->second,
  747.                                        $this->hour >= 12 ? 'pm' 'am');
  748.                     break;
  749.                 case 'R':
  750.                     $output .= sprintf('%02d:%02d',
  751.                                        $this->hour,
  752.                                        $this->minute);
  753.                     break;
  754.                 case 's':
  755.                     $output .= $this->second;
  756.                     break;
  757.                 case 'S':
  758.                     $output .= sprintf('%02d'$this->second);
  759.                     break;
  760.                 case 't':
  761.                     $output .= "\t";
  762.                     break;
  763.                 case 'T':
  764.                     $output .= sprintf('%02d:%02d:%02d',
  765.                                        $this->hour,
  766.                                        $this->minute,
  767.                                        $this->second);
  768.                     break;
  769.                 case '%':
  770.                     $output .= "%";
  771.                     break;
  772.                 default:
  773.                     $output .= $char $nextchar;
  774.                 }
  775.             else {
  776.                 $output .= $char;
  777.             }
  778.         }
  779.         return $output;
  780.     }
  781.  
  782.  
  783.     // }}}
  784.     // {{{ toSeconds()
  785.  
  786.     /**
  787.      * Convert time span to seconds
  788.      *
  789.      * @return   int        time span as an integer number of seconds
  790.      * @access   public
  791.      */
  792.     function toSeconds()
  793.     {
  794.         return $this->day * 86400 + $this->hour * 3600 +
  795.             $this->minute * 60 + $this->second;
  796.     }
  797.  
  798.  
  799.     // }}}
  800.     // {{{ toMinutes()
  801.  
  802.     /**
  803.      * Convert time span to minutes
  804.      *
  805.      * @return   float      time span as a decimal number of minutes
  806.      * @access   public
  807.      */
  808.     function toMinutes()
  809.     {
  810.         return $this->day * 1440 + $this->hour * 60 + $this->minute +
  811.             $this->second / 60;
  812.     }
  813.  
  814.  
  815.     // }}}
  816.     // {{{ toHours()
  817.  
  818.     /**
  819.      * Convert time span to hours
  820.      *
  821.      * @return   float      time span as a decimal number of hours
  822.      * @access   public
  823.      */
  824.     function toHours()
  825.     {
  826.         return $this->day * 24 + $this->hour $this->minute / 60 +
  827.             $this->second / 3600;
  828.     }
  829.  
  830.  
  831.     // }}}
  832.     // {{{ toDays()
  833.  
  834.     /**
  835.      * Convert time span to days
  836.      *
  837.      * @return   float      time span as a decimal number of days
  838.      * @access   public
  839.      */
  840.     function toDays()
  841.     {
  842.         return $this->day $this->hour / 24 + $this->minute / 1440 +
  843.             $this->second / 86400;
  844.     }
  845.  
  846.  
  847.     // }}}
  848.     // {{{ add()
  849.  
  850.     /**
  851.      * Adds a time span
  852.      *
  853.      * @param object $time time span to add
  854.      *
  855.      * @return   void 
  856.      * @access   public
  857.      */
  858.     function add($time)
  859.     {
  860.         return $this->setFromSeconds($this->toSeconds(+
  861.                                      $time->toSeconds());
  862.     }
  863.  
  864.  
  865.     // }}}
  866.     // {{{ subtract()
  867.  
  868.     /**
  869.      * Subtracts a time span
  870.      *
  871.      * If the time span to subtract is larger than the original, the result
  872.      * is zero (there's no sense in negative time spans).
  873.      *
  874.      * @param object $time time span to subtract
  875.      *
  876.      * @return   void 
  877.      * @access   public
  878.      */
  879.     function subtract($time)
  880.     {
  881.         $sub $this->toSeconds($time->toSeconds();
  882.         if ($sub < 0{
  883.             $this->setFromSeconds(0);
  884.         else {
  885.             $this->setFromSeconds($sub);
  886.         }
  887.     }
  888.  
  889.  
  890.     // }}}
  891.     // {{{ equal()
  892.  
  893.     /**
  894.      * Tells if time span is equal to $time
  895.      *
  896.      * @param object $time time span to compare to
  897.      *
  898.      * @return   bool       true if the time spans are equal
  899.      * @access   public
  900.      */
  901.     function equal($time)
  902.     {
  903.         return $this->toSeconds(== $time->toSeconds();
  904.     }
  905.  
  906.  
  907.     // }}}
  908.     // {{{ greaterEqual()
  909.  
  910.     /**
  911.      * Tells if this time span is greater or equal than $time
  912.      *
  913.      * @param object $time time span to compare to
  914.      *
  915.      * @return   bool       true if this time span is greater or equal than $time
  916.      * @access   public
  917.      */
  918.     function greaterEqual($time)
  919.     {
  920.         return $this->toSeconds(>= $time->toSeconds();
  921.     }
  922.  
  923.  
  924.     // }}}
  925.     // {{{ lowerEqual()
  926.  
  927.     /**
  928.      * Tells if this time span is lower or equal than $time
  929.      *
  930.      * @param object $time time span to compare to
  931.      *
  932.      * @return   bool       true if this time span is lower or equal than $time
  933.      * @access   public
  934.      */
  935.     function lowerEqual($time)
  936.     {
  937.         return $this->toSeconds(<= $time->toSeconds();
  938.     }
  939.  
  940.  
  941.     // }}}
  942.     // {{{ greater()
  943.  
  944.     /**
  945.      * Tells if this time span is greater than $time
  946.      *
  947.      * @param object $time time span to compare to
  948.      *
  949.      * @return   bool       true if this time span is greater than $time
  950.      * @access   public
  951.      */
  952.     function greater($time)
  953.     {
  954.         return $this->toSeconds($time->toSeconds();
  955.     }
  956.  
  957.  
  958.     // }}}
  959.     // {{{ lower()
  960.  
  961.     /**
  962.      * Tells if this time span is lower than $time
  963.      *
  964.      * @param object $time time span to compare to
  965.      *
  966.      * @return   bool       true if this time span is lower than $time
  967.      * @access   public
  968.      */
  969.     function lower($time)
  970.     {
  971.         return $this->toSeconds($time->toSeconds();
  972.     }
  973.  
  974.  
  975.     // }}}
  976.     // {{{ compare()
  977.  
  978.     /**
  979.      * Compares two time spans
  980.      *
  981.      * Suitable for use in sorting functions.
  982.      *
  983.      * @param object $time1 the first time span
  984.      * @param object $time2 the second time span
  985.      *
  986.      * @return   int        0 if the time spans are equal, -1 if time1 is lower
  987.      *                        than time2, 1 if time1 is greater than time2
  988.      * @access   public
  989.      * @static
  990.      */
  991.     function compare($time1$time2)
  992.     {
  993.         if ($time1->equal($time2)) {
  994.             return 0;
  995.         elseif ($time1->lower($time2)) {
  996.             return -1;
  997.         else {
  998.             return 1;
  999.         }
  1000.     }
  1001.  
  1002.  
  1003.     // }}}
  1004.     // {{{ isEmpty()
  1005.  
  1006.     /**
  1007.      * Tells if the time span is empty (zero length)
  1008.      *
  1009.      * @return   bool       true if empty
  1010.      * @access   public
  1011.      */
  1012.     function isEmpty()
  1013.     {
  1014.         return !$this->day && !$this->hour && !$this->minute && !$this->second;
  1015.     }
  1016.  
  1017.  
  1018.     // }}}
  1019.     // {{{ setDefaultInputFormat()
  1020.  
  1021.     /**
  1022.      * Set the default input format
  1023.      *
  1024.      * @param mixed $format new default input format
  1025.      *
  1026.      * @return   mixed      previous default input format
  1027.      * @access   public
  1028.      * @static
  1029.      */
  1030.     function setDefaultInputFormat($format)
  1031.     {
  1032.         $old $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
  1033.         $GLOBALS['_DATE_SPAN_INPUT_FORMAT'$format;
  1034.         return $old;
  1035.     }
  1036.  
  1037.  
  1038.     // }}}
  1039.     // {{{ getDefaultInputFormat()
  1040.  
  1041.     /**
  1042.      * Get the default input format
  1043.      *
  1044.      * @return   mixed      default input format
  1045.      * @access   public
  1046.      * @static
  1047.      */
  1048.     function getDefaultInputFormat()
  1049.     {
  1050.         return $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
  1051.     }
  1052.  
  1053.  
  1054.     // }}}
  1055.     // {{{ setDefaultFormat()
  1056.  
  1057.     /**
  1058.      * Set the default format
  1059.      *
  1060.      * @param mixed $format new default format
  1061.      *
  1062.      * @return   mixed      previous default format
  1063.      * @access   public
  1064.      * @static
  1065.      */
  1066.     function setDefaultFormat($format)
  1067.     {
  1068.         $old $GLOBALS['_DATE_SPAN_FORMAT'];
  1069.         $GLOBALS['_DATE_SPAN_FORMAT'$format;
  1070.         return $old;
  1071.     }
  1072.  
  1073.  
  1074.     // }}}
  1075.     // {{{ getDefaultFormat()
  1076.  
  1077.     /**
  1078.      * Get the default format
  1079.      *
  1080.      * @return   mixed      default format
  1081.      * @access   public
  1082.      * @static
  1083.      */
  1084.     function getDefaultFormat()
  1085.     {
  1086.         return $GLOBALS['_DATE_SPAN_FORMAT'];
  1087.     }
  1088.  
  1089.  
  1090.     // }}}
  1091.  
  1092. }
  1093.  
  1094. // }}}
  1095.  
  1096. /*
  1097.  * Local variables:
  1098.  * mode: php
  1099.  * tab-width: 4
  1100.  * c-basic-offset: 4
  1101.  * c-hanging-comment-ender-p: nil
  1102.  * End:
  1103.  */
  1104. ?>

Documentation generated on Sun, 23 Mar 2008 20:00:33 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.