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

Source for file dateTime.php

Documentation is available at dateTime.php

  1. <?php
  2. //
  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: Shane Caraveo <Shane@Caraveo.com>   Port to PEAR and more   |
  17. // | Authors: Dietrich Ayala <dietrich@ganx4.com> Original Author         |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: dateTime.php,v 1.7 2005/03/10 23:16:39 yunosh Exp $
  21. //
  22. class SOAP_Type_dateTime
  23. {
  24.     var $ereg_iso8601 '(-?[0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(\.[0-9]*)?(Z|[+\-][0-9]{4}|[+\-][0-9]{2}:[0-9]{2})?';
  25.         #"(-?[0-9]{4})-".  // centuries & years CCYY-
  26.         #"([0-9]{2})-".  // months MM-
  27.         #"([0-9]{2})".    // days DD
  28.         #"T".                    // separator T
  29.         #"([0-9]{2}):".  // hours hh:
  30.         #"([0-9]{2}):".  // minutes mm:
  31.         #"([0-9]{2})(\.[0-9]*)?". // seconds ss.ss...
  32.         #"(Z|[+\-][0-9]{4}|[+\-][0-9]{2}:[0-9]{2})?"; // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
  33.         # if no 8th reg (Z) assumes UTC
  34.     var $timestamp = -1;
  35.     
  36.     function SOAP_Type_dateTime($date = -1)
  37.     {
  38.         if ($date == -1$date time();
  39.  
  40.         if (gettype($date== 'integer'{
  41.             $this->timestamp $date;
  42.         else {
  43.             $this->timestamp $this->toUnixtime($date);
  44.         }
  45.     }
  46.     
  47.     function toSOAP($date = NULL)
  48.     {
  49.         return $this->toUTC($date);
  50.     }
  51.     
  52.     function toString($timestamp = 0)
  53.     {
  54.         if (!$timestamp$timestamp $this->timestamp;
  55.         if ($timestamp < 0return 0;
  56.  
  57.         return date('Y-m-d\TH:i:sO',$timestamp);
  58.     }
  59.     
  60.     function _split($datestr)
  61.     {
  62.         if (!$datestr)
  63.             $datestr $this->toString();
  64.         else if (gettype($datestr== 'integer')
  65.             $datestr $this->toString($datestr);
  66.             
  67.         if (ereg($this->ereg_iso8601,$datestr,$regs)) {
  68.             if ($regs[8!= '' && $regs[8!= 'Z'{
  69.                 $op substr($regs[8],0,1);
  70.                 $h substr($regs[8],1,2);
  71.                 if (strstr($regs[8],':')) {
  72.                     $m substr($regs[8],4,2);
  73.                 else {
  74.                     $m substr($regs[8],3,2);
  75.                 }
  76.                 if ($op == '+'{
  77.                     $regs[4$regs[4$h;
  78.                     if ($regs[4< 0$regs[4+= 24;
  79.                     $regs[5$regs[5$m;
  80.                     if ($regs[5< 0$regs[5+= 60;
  81.                 else {
  82.                     $regs[4$regs[4$h;
  83.                     if ($regs[4> 23$regs[4-= 24;
  84.                     $regs[5$regs[5$m;
  85.                     if ($regs[5> 59$regs[5-= 60;
  86.                 }
  87.             }
  88.             return $regs;
  89.         }
  90.         return FALSE;
  91.     }
  92.     
  93.     function toUTC($datestr = NULL)
  94.     {
  95.         $regs $this->_split($datestr);
  96.  
  97.         if ($regs{
  98.             return sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',$regs[1],$regs[2],$regs[3],$regs[4],$regs[5],$regs[6]);
  99.         }
  100.  
  101.         return '';
  102.     }
  103.  
  104.     function toUnixtime($datestr = NULL)
  105.     {
  106.         $regs $this->_split($datestr);
  107.         if ($regs{
  108.             return strtotime("$regs[1]-$regs[2]-$regs[3] $regs[4]:$regs[5]:$regs[6]Z");
  109.         }
  110.         return -1;
  111.     }
  112.  
  113.     function compare($date1$date2 = NULL)
  114.     {
  115.         if ($date2 === null{
  116.             $date2 $date1;
  117.             $date1 $this->timestamp;
  118.         }
  119.         if (!is_numeric($date1))
  120.             $date1 $this->toUnixtime($date1);
  121.         if (!is_numeric($date2))
  122.             $date2 $this->toUnixtime($date2);
  123.         if ($date1 != -1 && $date2 != -1return $date1 $date2;
  124.         return -1;
  125.     }
  126.     
  127.     function _test($orig '2001-04-25T09:31:41-0700')
  128.     {
  129.         $utc $this->toUTC($orig);
  130.         $ts1 $this->toUnixtime($orig);
  131.         $ts2 $this->toUnixtime($utc);
  132.         $b1 $this->toString($ts1);
  133.         $b2 $this->toString($ts2);
  134.         print "orig: $orig\n";
  135.         print "orig toUTC: $utc\n";
  136.         print "orig ts: $ts1\n";
  137.         print "utc ts: $ts2\n";
  138.         print "orig ts back: $b1\n";
  139.         print "utc ts back: $b2\n";
  140.         if ($b1 != $orig || $b2 != $orig{
  141.             echo "Error in iso8601 conversions\n";
  142.         else {
  143.             echo "dateTime OK\n";
  144.         }
  145.     }
  146. }
  147.  
  148. # these should all match
  149. #$d = new SOAP_Type_dateTime();
  150. #echo $d->compare("2001-04-25T20:31:41Z","2001-04-25T20:31:41Z")."\n";
  151. #echo $d->compare("2001-04-25T20:31:41Z","2001-04-25T12:31:41-0800")."\n";
  152. #echo $d->compare("2001-04-25T20:31:41Z","2001-04-25T12:31:41-08:00")."\n";
  153.  
  154. ?>

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