WIP - more fixes
[plcapi.git] / src / Helper / Date.php
1 <?php
2
3 namespace PhpXmlRpc\Helper;
4
5 class Date
6 {
7     /**
8      * Given a timestamp, return the corresponding ISO8601 encoded string.
9      *
10      * Really, timezones ought to be supported
11      * but the XML-RPC spec says:
12      *
13      * "Don't assume a timezone. It should be specified by the server in its
14      * documentation what assumptions it makes about timezones."
15      *
16      * These routines always assume localtime unless
17      * $utc is set to 1, in which case UTC is assumed
18      * and an adjustment for locale is made when encoding
19      *
20      * @param int $timet (timestamp)
21      * @param int $utc (0 or 1)
22      * @return string
23      */
24     public static function iso8601_encode($timet, $utc=0)
25     {
26         if(!$utc)
27         {
28             $t=strftime("%Y%m%dT%H:%M:%S", $timet);
29         }
30         else
31         {
32             if(function_exists('gmstrftime'))
33             {
34                 // gmstrftime doesn't exist in some versions
35                 // of PHP
36                 $t=gmstrftime("%Y%m%dT%H:%M:%S", $timet);
37             }
38             else
39             {
40                 $t=strftime("%Y%m%dT%H:%M:%S", $timet-date('Z'));
41             }
42         }
43         return $t;
44     }
45
46     /**
47      * Given an ISO8601 date string, return a timet in the localtime, or UTC
48      * @param string $idate
49      * @param int $utc either 0 or 1
50      * @return int (datetime)
51      */
52     public static function iso8601_decode($idate, $utc=0)
53     {
54         $t=0;
55         if(preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/', $idate, $regs))
56         {
57             if($utc)
58             {
59                 $t=gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
60             }
61             else
62             {
63                 $t=mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
64             }
65         }
66         return $t;
67     }
68 }