home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / DB / Table / Date.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  6.2 KB  |  196 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Generic date handling class for DB_Table.
  7.  * 
  8.  * Stripped down to two essential methods specially for DB_Table from the
  9.  * PEAR Date package by Paul M. Jones <pmjones@php.net>.
  10.  *
  11.  * PHP versions 4 and 5
  12.  *
  13.  * LICENSE:
  14.  * 
  15.  * Copyright (c) 1997-2007, Paul M. Jones <pmjones@php.net>
  16.  *                          David C. Morse <morse@php.net>
  17.  *                          Mark Wiesemann <wiesemann@php.net>
  18.  * All rights reserved.
  19.  *
  20.  * Redistribution and use in source and binary forms, with or without
  21.  * modification, are permitted provided that the following conditions
  22.  * are met:
  23.  *
  24.  *    * Redistributions of source code must retain the above copyright
  25.  *      notice, this list of conditions and the following disclaimer.
  26.  *    * Redistributions in binary form must reproduce the above copyright
  27.  *      notice, this list of conditions and the following disclaimer in the 
  28.  *      documentation and/or other materials provided with the distribution.
  29.  *    * The names of the authors may not be used to endorse or promote products 
  30.  *      derived from this software without specific prior written permission.
  31.  *
  32.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  37.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  38.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  39.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  40.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  41.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  42.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  *
  44.  * @category Database
  45.  * @package  DB_Table
  46.  * @author   Baba Buehler <baba@babaz.com>
  47.  * @author   Pierre-Alain Joye <pajoye@php.net>
  48.  * @license  http://opensource.org/licenses/bsd-license.php New BSD License
  49.  * @version  CVS: $Id: Date.php,v 1.3 2007/12/13 16:52:14 wiesemann Exp $
  50.  * @link     http://pear.php.net/package/DB_Table
  51.  */
  52.  
  53. /**
  54.  * Generic date handling class for DB_Table.
  55.  *
  56.  * @category Database
  57.  * @package  DB_Table
  58.  * @author   Baba Buehler <baba@babaz.com>
  59.  * @author   Pierre-Alain Joye <pajoye@php.net>
  60.  * @version  Release: 1.5.5
  61.  * @link     http://pear.php.net/package/DB_Table
  62.  */
  63. class DB_Table_Date {
  64.     
  65.     /**
  66.      * the year
  67.      * @var int
  68.      */
  69.     var $year;
  70.     /**
  71.      * the month
  72.      * @var int
  73.      */
  74.     var $month;
  75.     /**
  76.      * the day
  77.      * @var int
  78.      */
  79.     var $day;
  80.     /**
  81.      * the hour
  82.      * @var int
  83.      */
  84.     var $hour;
  85.     /**
  86.      * the minute
  87.      * @var int
  88.      */
  89.     var $minute;
  90.     /**
  91.      * the second
  92.      * @var int
  93.      */
  94.     var $second;
  95.     /**
  96.      * the parts of a second
  97.      * @var float
  98.      */
  99.     var $partsecond;
  100.     
  101.     /**
  102.      * Constructor
  103.      *
  104.      * Creates a new DB_Table_Date Object. The date should be near to
  105.      * ISO 8601 format.
  106.      *
  107.      * @access public
  108.      * @param string $date A date in ISO 8601 format.
  109.      */
  110.     function DB_Table_Date($date)
  111.     {
  112.         // This regex is very loose and accepts almost any butchered
  113.         // format you could throw at it.  e.g. 2003-10-07 19:45:15 and
  114.         // 2003-10071945:15 are the same thing in the eyes of this
  115.         // regex, even though the latter is not a valid ISO 8601 date.
  116.         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);
  117.         $this->year       = $regs[1];
  118.         $this->month      = $regs[2];
  119.         $this->day        = $regs[3];
  120.         $this->hour       = isset($regs[5])?$regs[5]:0;
  121.         $this->minute     = isset($regs[6])?$regs[6]:0;
  122.         $this->second     = isset($regs[7])?$regs[7]:0;
  123.         $this->partsecond = isset($regs[8])?(float)$regs[8]:(float)0;
  124.  
  125.         // if an offset is defined, convert time to UTC
  126.         // Date currently can't set a timezone only by offset,
  127.         // so it has to store it as UTC
  128.         if (isset($regs[9])) {
  129.             $this->toUTCbyOffset($regs[9]);
  130.         }
  131.     }
  132.     
  133.     
  134.     /**
  135.      *  Date pretty printing, similar to strftime()
  136.      *
  137.      *  Formats the date in the given format, much like
  138.      *  strftime().  Most strftime() options are supported.<br><br>
  139.      *
  140.      *  formatting options:<br><br>
  141.      *
  142.      *  <code>%Y  </code>  year as decimal including century (range 0000 to 9999) <br>
  143.      *  <code>%m  </code>  month as decimal number (range 01 to 12) <br>
  144.      *  <code>%d  </code>  day of month (range 00 to 31) <br>
  145.      *  <code>%H  </code>  hour as decimal number (00 to 23) <br>
  146.      *  <code>%M  </code>  minute as a decimal number (00 to 59) <br>
  147.      *  <code>%S  </code>  seconds as a decimal number (00 to 59) <br>
  148.      *  <code>%%  </code>  literal '%' <br>
  149.      * <br>
  150.      *
  151.      * @access public
  152.      * @param string format the format string for returned date/time
  153.      * @return string date/time in given format
  154.      */
  155.     function format($format)
  156.     {
  157.         $output = "";
  158.  
  159.         for($strpos = 0; $strpos < strlen($format); $strpos++) {
  160.             $char = substr($format,$strpos,1);
  161.             if ($char == "%") {
  162.                 $nextchar = substr($format,$strpos + 1,1);
  163.                 switch ($nextchar) {
  164.                 case "Y":
  165.                     $output .= $this->year;
  166.                     break;
  167.                 case "m":
  168.                     $output .= sprintf("%02d",$this->month);
  169.                     break;
  170.                 case "d":
  171.                     $output .= sprintf("%02d",$this->day);
  172.                     break;
  173.                 case "H":
  174.                     $output .= sprintf("%02d", $this->hour);
  175.                     break;
  176.                 case "M":
  177.                     $output .= sprintf("%02d",$this->minute);
  178.                     break;
  179.                 case "S":
  180.                     $output .= sprintf("%02d", $this->second);
  181.                     break;
  182.                 default:
  183.                     $output .= $char.$nextchar;
  184.                 }
  185.                 $strpos++;
  186.             } else {
  187.                 $output .= $char;
  188.             }
  189.         }
  190.         return $output;
  191.  
  192.     }
  193. }
  194.  
  195. ?>
  196.