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

Source for file Date.php

Documentation is available at Date.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  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. // | Authors: Baba Buehler <baba@babaz.com>                               |
  17. // |          Pierre-Alain Joye <pajoye@php.net>                          |
  18. // |                                                                      |
  19. // | Stripped down to two essential methods specially for DB_Table        |
  20. // | under PHP 5.1 by Paul M. Jones <pmjones@php.net>                     |
  21. // +----------------------------------------------------------------------+
  22. //
  23. // $Id: Date.php,v 1.1 2005/12/05 21:07:53 wiesemann Exp $
  24.  
  25. /**
  26.  * Generic date handling class for DB_Table.
  27.  *
  28.  * @author Baba Buehler <baba@babaz.com>
  29.  * @package Date
  30.  * @access public
  31.  */
  32. class DB_Table_Date {
  33.     
  34.     /**
  35.      * the year
  36.      * @var int 
  37.      */
  38.     var $year;
  39.     /**
  40.      * the month
  41.      * @var int 
  42.      */
  43.     var $month;
  44.     /**
  45.      * the day
  46.      * @var int 
  47.      */
  48.     var $day;
  49.     /**
  50.      * the hour
  51.      * @var int 
  52.      */
  53.     var $hour;
  54.     /**
  55.      * the minute
  56.      * @var int 
  57.      */
  58.     var $minute;
  59.     /**
  60.      * the second
  61.      * @var int 
  62.      */
  63.     var $second;
  64.     /**
  65.      * the parts of a second
  66.      * @var float 
  67.      */
  68.     var $partsecond;
  69.     
  70.     /**
  71.      * Constructor
  72.      *
  73.      * Creates a new DB_Table_Date Object. The date should be near to
  74.      * ISO 8601 format.
  75.      *
  76.      * @access public
  77.      * @param string $date A date in ISO 8601 format.
  78.      */
  79.     function DB_Table_Date($date)
  80.     {
  81.         // This regex is very loose and accepts almost any butchered
  82.         // format you could throw at it.  e.g. 2003-10-07 19:45:15 and
  83.         // 2003-10071945:15 are the same thing in the eyes of this
  84.         // regex, even though the latter is not a valid ISO 8601 date.
  85.         preg_match('/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)?$/i'$date$regs);
  86.         $this->year       = $regs[1];
  87.         $this->month      = $regs[2];
  88.         $this->day        = $regs[3];
  89.         $this->hour       = isset($regs[5])?$regs[5]:0;
  90.         $this->minute     = isset($regs[6])?$regs[6]:0;
  91.         $this->second     = isset($regs[7])?$regs[7]:0;
  92.         $this->partsecond = isset($regs[8])?(float)$regs[8]:(float)0;
  93.  
  94.         // if an offset is defined, convert time to UTC
  95.         // Date currently can't set a timezone only by offset,
  96.         // so it has to store it as UTC
  97.         if (isset($regs[9])) {
  98.             $this->toUTCbyOffset($regs[9]);
  99.         }
  100.     }
  101.     
  102.     
  103.     /**
  104.      *  Date pretty printing, similar to strftime()
  105.      *
  106.      *  Formats the date in the given format, much like
  107.      *  strftime().  Most strftime() options are supported.<br><br>
  108.      *
  109.      *  formatting options:<br><br>
  110.      *
  111.      *  <code>%Y  </code>  year as decimal including century (range 0000 to 9999) <br>
  112.      *  <code>%m  </code>  month as decimal number (range 01 to 12) <br>
  113.      *  <code>%d  </code>  day of month (range 00 to 31) <br>
  114.      *  <code>%H  </code>  hour as decimal number (00 to 23) <br>
  115.      *  <code>%M  </code>  minute as a decimal number (00 to 59) <br>
  116.      *  <code>%S  </code>  seconds as a decimal number (00 to 59) <br>
  117.      *  <code>%%  </code>  literal '%' <br>
  118.      * <br>
  119.      *
  120.      * @access public
  121.      * @param string format the format string for returned date/time
  122.      * @return string date/time in given format
  123.      */
  124.     function format($format)
  125.     {
  126.         $output "";
  127.  
  128.         for($strpos = 0; $strpos strlen($format)$strpos++{
  129.             $char substr($format,$strpos,1);
  130.             if ($char == "%"{
  131.                 $nextchar substr($format,$strpos + 1,1);
  132.                 switch ($nextchar{
  133.                 case "Y":
  134.                     $output .= $this->year;
  135.                     break;
  136.                 case "m":
  137.                     $output .= sprintf("%02d",$this->month);
  138.                     break;
  139.                 case "d":
  140.                     $output .= sprintf("%02d",$this->day);
  141.                     break;
  142.                 case "H":
  143.                     $output .= sprintf("%02d"$this->hour);
  144.                     break;
  145.                 case "M":
  146.                     $output .= sprintf("%02d",$this->minute);
  147.                     break;
  148.                 case "S":
  149.                     $output .= sprintf("%02d"$this->second);
  150.                     break;
  151.                 default:
  152.                     $output .= $char.$nextchar;
  153.                 }
  154.                 $strpos++;
  155.             else {
  156.                 $output .= $char;
  157.             }
  158.         }
  159.         return $output;
  160.  
  161.     }
  162. }
  163.  
  164. ?>

Documentation generated on Mon, 11 Mar 2019 14:42:33 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.