1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpXmlRpc\Helper; |
4
|
|
|
|
5
|
|
|
use PhpXmlRpc\PhpXmlRpc; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Helps to convert timestamps to the xml-rpc date format. |
9
|
|
|
* |
10
|
|
|
* Feature creep -- add support for custom TZs |
11
|
|
|
*/ |
12
|
|
|
class Date |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Given a timestamp, return the corresponding ISO8601 encoded string. |
16
|
|
|
* |
17
|
|
|
* Really, timezones ought to be supported but the XML-RPC spec says: |
18
|
|
|
* |
19
|
|
|
* "Don't assume a timezone. It should be specified by the server in its documentation what assumptions it makes |
20
|
|
|
* about timezones." |
21
|
|
|
* |
22
|
|
|
* This routine always encodes to local time unless $utc is set to 1, in which case UTC output is produced and an |
23
|
23 |
|
* adjustment for the local timezone's offset is made |
24
|
|
|
* |
25
|
23 |
|
* @param int|\DateTimeInterface $timet timestamp or datetime |
26
|
23 |
|
* @param bool|int $utc (0 or 1) |
27
|
|
|
* @return string |
28
|
1 |
|
*/ |
29
|
|
|
public static function iso8601Encode($timet, $utc = 0) |
30
|
|
|
{ |
31
|
23 |
|
if (is_a($timet, 'DateTimeInterface') || is_a($timet, 'DateTime')) { |
32
|
|
|
$timet = $timet->getTimestamp(); |
33
|
|
|
} |
34
|
|
|
if (!$utc) { |
35
|
|
|
$t = date('Ymd\TH:i:s', $timet); |
36
|
|
|
} else { |
37
|
|
|
$t = gmdate('Ymd\TH:i:s', $timet); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $t; |
41
|
|
|
} |
42
|
1 |
|
|
43
|
|
|
/** |
44
|
1 |
|
* Given an ISO8601 date string, return a timestamp in the localtime, or UTC. |
45
|
1 |
|
* |
46
|
1 |
|
* @param string $idate |
47
|
|
|
* @param bool|int $utc either 0 (assume date is in local time) or 1 (assume date is in UTC) |
48
|
|
|
* |
49
|
1 |
|
* @return int (timestamp) 0 if the source string does not match the xml-rpc dateTime format |
50
|
|
|
*/ |
51
|
|
|
public static function iso8601Decode($idate, $utc = 0) |
52
|
|
|
{ |
53
|
1 |
|
$t = 0; |
54
|
|
|
if (preg_match(PhpXmlRpc::$xmlrpc_datetime_format, $idate, $regs)) { |
55
|
|
|
if ($utc) { |
56
|
|
|
$t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); |
57
|
|
|
} else { |
58
|
|
|
$t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $t; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|