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

Source for file dateTime.php

Documentation is available at dateTime.php

  1. <?php
  2. /**
  3.  * This file contains the code for the SOAP_Type_dateTime class.
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * LICENSE: This source file is subject to version 2.02 of the PHP license,
  8.  * that is bundled with this package in the file LICENSE, and is available at
  9.  * through the world-wide-web at http://www.php.net/license/2_02.txt.  If you
  10.  * did not receive a copy of the PHP license and are unable to obtain it
  11.  * through the world-wide-web, please send a note to license@php.net so we can
  12.  * mail you a copy immediately.
  13.  *
  14.  * @category   Web Services
  15.  * @package    SOAP
  16.  * @author     Dietrich Ayala <dietrich@ganx4.com> Original Author
  17.  * @author     Shane Caraveo <Shane@Caraveo.com>   Port to PEAR and more
  18.  * @author     Jan Schneider <jan@horde.org>       Maintenance
  19.  * @copyright  2003-2007 The PHP Group
  20.  * @license    http://www.php.net/license/2_02.txt  PHP License 2.02
  21.  * @link       http://pear.php.net/package/SOAP
  22.  */
  23.  
  24. /**
  25.  * This class converts from and to unix timestamps and ISO 8601 date/time.
  26.  *
  27.  * @access   public
  28.  * @package  SOAP
  29.  * @author   Dietrich Ayala <dietrich@ganx4.com> Original Author
  30.  * @author   Shane Caraveo <shane@php.net>       Port to PEAR and more
  31.  * @author   Jan Schneider <jan@horde.org>       Maintenance
  32.  */
  33. {
  34.     var $_iso8601 =
  35.         '# 1: centuries & years CCYY-
  36.          (-?[0-9]{4})-
  37.          # 2: months MM-
  38.          ([0-9]{2})-
  39.          # 3: days DD
  40.          ([0-9]{2})
  41.          # 4: separator T
  42.          T
  43.          # 5: hours hh:
  44.          ([0-9]{2}):
  45.          # 6: minutes mm:
  46.          ([0-9]{2}):
  47.          # 7: seconds ss.ss...
  48.          ([0-9]{2})(\.[0-9]*)?
  49.          # 8: Z to indicate UTC, -+HH:MM:SS.SS... for local zones
  50.          (Z|[+\-][0-9]{4}|[+\-][0-9]{2}:[0-9]{2})?';
  51.  
  52.     var $timestamp = -1;
  53.  
  54.     /**
  55.      * Constructor.
  56.      *
  57.      * @param string|integer$date  The timestamp or ISO 8601 formatted
  58.      *                               date and time this object is going to
  59.      *                               represent.
  60.      */
  61.     function SOAP_Type_dateTime($date = -1)
  62.     {
  63.         if ($date == -1{
  64.             $this->timestamp = time();
  65.         elseif (is_int($date)) {
  66.             $this->timestamp = $date;
  67.         else {
  68.             $this->timestamp = $this->toUnixtime($date);
  69.         }
  70.     }
  71.  
  72.     /**
  73.      * Alias of {@link SOAP_Type_dateTime::toUTC}.
  74.      */
  75.     function toSOAP($date = NULL)
  76.     {
  77.         return $this->toUTC($date);
  78.     }
  79.  
  80.     /**
  81.      * Converts this object or a timestamp to an ISO 8601 date/time string.
  82.      *
  83.      * @param integer $timestamp  A unix timestamp
  84.      *
  85.      * @return string  An ISO 8601 formatted date/time string.
  86.      */
  87.     function toString($timestamp = 0)
  88.     {
  89.         if (!$timestamp{
  90.             $timestamp $this->timestamp;
  91.         }
  92.         if ($timestamp < 0{
  93.             return 0;
  94.         }
  95.  
  96.         //simulate PHP5's P parameter
  97.         $zone date('O'$timestamp);
  98.         if (strlen($zone=== 5{
  99.             $zone substr($zone03':' substr($zone3);
  100.         }
  101.         return date('Y-m-d\TH:i:s'$timestamp$zone;
  102.     }
  103.  
  104.     /**
  105.      * Splits a date/time into its components.
  106.      *
  107.      * @param string|integer$datestr  A unix timestamp or ISO 8601 date/time
  108.      *                                  string. If empty, this object is used.
  109.      *
  110.      * @return boolean|array An array with the date and time components or
  111.      *                         false on failure.
  112.      */
  113.     function _split($datestr)
  114.     {
  115.         if (!$datestr{
  116.             $datestr $this->toString();
  117.         elseif (is_int($datestr)) {
  118.             $datestr $this->toString($datestr);
  119.         }
  120.  
  121.         if (preg_match('/' $this->_iso8601 '/x'$datestr$regs)) {
  122.             if (empty($regs[8])) {
  123.                 $timestamp strtotime(sprintf('%04d-%02d-%02d %02d:%02d:%02d',
  124.                                                $regs[1],
  125.                                                $regs[2],
  126.                                                $regs[3],
  127.                                                $regs[4],
  128.                                                $regs[5],
  129.                                                $regs[6]));
  130.                 $regs[8date('O'$timestamp);
  131.             }
  132.             if ($regs[8!= 'Z'{
  133.                 $op substr($regs[8]01);
  134.                 $h substr($regs[8]12);
  135.                 if (strstr($regs[8]':')) {
  136.                     $m substr($regs[8]42);
  137.                 else {
  138.                     $m substr($regs[8]32);
  139.                 }
  140.                 if ($op == '+'{
  141.                     $regs[4$regs[4$h;
  142.                     if ($regs[4< 0{
  143.                         $regs[4+= 24;
  144.                     }
  145.                     $regs[5$regs[5$m;
  146.                     if ($regs[5< 0{
  147.                         $regs[5+= 60;
  148.                     }
  149.                 else {
  150.                     $regs[4$regs[4$h;
  151.                     if ($regs[4> 23{
  152.                         $regs[4-= 24;
  153.                     }
  154.                     $regs[5$regs[5$m;
  155.                     if ($regs[5> 59{
  156.                         $regs[5-= 60;
  157.                     }
  158.                 }
  159.             }
  160.             return $regs;
  161.         }
  162.  
  163.         return false;
  164.     }
  165.  
  166.     /**
  167.      * Returns an ISO 8601 formatted UTC date/time string.
  168.      *
  169.      * @param string|integer$datestr  @see SOAP_Type_dateTime::_split
  170.      *
  171.      * @return string  The ISO 8601 formatted UTC date/time string.
  172.      */
  173.     function toUTC($datestr = null)
  174.     {
  175.         $regs $this->_split($datestr);
  176.  
  177.         if ($regs{
  178.             return sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',
  179.                            $regs[1],
  180.                            $regs[2],
  181.                            $regs[3],
  182.                            $regs[4],
  183.                            $regs[5],
  184.                            $regs[6]);
  185.         }
  186.  
  187.         return '';
  188.     }
  189.  
  190.     /**
  191.      * Returns a unix timestamp.
  192.      *
  193.      * @param string|integer$datestr  @see SOAP_Type_dateTime::_split
  194.      *
  195.      * @return integer  The unix timestamp.
  196.      */
  197.     function toUnixtime($datestr = null)
  198.     {
  199.         $regs $this->_split($datestr);
  200.         if ($regs{
  201.             return strtotime(sprintf('%04d-%02d-%02d %02d:%02d:%02dZ',
  202.                                      $regs[1],
  203.                                      $regs[2],
  204.                                      $regs[3],
  205.                                      $regs[4],
  206.                                      $regs[5],
  207.                                      $regs[6]));
  208.         }
  209.         return -1;
  210.     }
  211.  
  212.     /**
  213.      * Compares two dates or this object with a second date.
  214.      *
  215.      * @param string|integer$date1  A unix timestamp or ISO 8601 date/time
  216.      *                                string.
  217.      * @param string|integer$date2  A unix timestamp or ISO 8601 date/time
  218.      *                                string. If empty, this object is used.
  219.      *
  220.      * @return integer  The difference between the first and the second date.
  221.      */
  222.     function compare($date1$date2 = null)
  223.     {
  224.         if (is_null($date2)) {
  225.             $date2 $date1;
  226.             $date1 $this->timestamp;
  227.         }
  228.         if (!is_int($date1)) {
  229.             $date1 $this->toUnixtime($date1);
  230.         }
  231.         if (!is_int($date2)) {
  232.             $date2 $this->toUnixtime($date2);
  233.         }
  234.  
  235.         if ($date1 != -1 && $date2 != -1{
  236.             return $date1 $date2;
  237.         }
  238.  
  239.         return -1;
  240.     }
  241.  
  242. }

Documentation generated on Mon, 04 Aug 2008 20:00:15 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.