3
* This file contains the code for the SOAP_Type_dateTime class.
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.
14
* @category Web Services
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
25
* This class converts from and to unix timestamps and ISO 8601 date/time.
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
33
class SOAP_Type_dateTime
36
'# 1: centuries & years CCYY-
50
# 8: Z to indicate UTC, -+HH:MM:SS.SS... for local zones
51
(Z|[+\-][0-9]{4}|[+\-][0-9]{2}:[0-9]{2})?';
58
* @param string|integer $date The timestamp or ISO 8601 formatted
59
* date and time this object is going to
62
function SOAP_Type_dateTime($date = -1)
65
$this->timestamp = time();
66
} elseif (is_int($date)) {
67
$this->timestamp = $date;
69
$this->timestamp = $this->toUnixtime($date);
74
* Alias of {@link SOAP_Type_dateTime::toUTC}.
76
function toSOAP($date = NULL)
78
return $this->toUTC($date);
82
* Converts this object or a timestamp to an ISO 8601 date/time string.
84
* @param integer $timestamp A unix timestamp
86
* @return string An ISO 8601 formatted date/time string.
88
function toString($timestamp = 0)
91
$timestamp = $this->timestamp;
97
//simulate PHP5's P parameter
98
$zone = date('O', $timestamp);
99
if (strlen($zone) === 5) {
100
$zone = substr($zone, 0, 3) . ':' . substr($zone, 3);
102
return date('Y-m-d\TH:i:s', $timestamp) . $zone;
106
* Splits a date/time into its components.
108
* @param string|integer $datestr A unix timestamp or ISO 8601 date/time
109
* string. If empty, this object is used.
111
* @return boolean|array An array with the date and time components or
114
function _split($datestr)
117
$datestr = $this->toString();
118
} elseif (is_int($datestr)) {
119
$datestr = $this->toString($datestr);
122
if (preg_match('/' . $this->_iso8601 . '/x', $datestr, $regs)) {
123
if (empty($regs[8])) {
124
$timestamp = strtotime(sprintf('%04d-%02d-%02d %02d:%02d:%02d',
131
$regs[8] = date('O', $timestamp);
133
if ($regs[8] != 'Z') {
134
$op = substr($regs[8], 0, 1);
135
$h = substr($regs[8], 1, 2);
136
if (strstr($regs[8], ':')) {
137
$m = substr($regs[8], 4, 2);
139
$m = substr($regs[8], 3, 2);
142
$regs[4] = $regs[4] - $h;
146
$regs[5] = $regs[5] - $m;
151
$regs[4] = $regs[4] + $h;
155
$regs[5] = $regs[5] + $m;
168
* Returns an ISO 8601 formatted UTC date/time string.
170
* @param string|integer $datestr @see SOAP_Type_dateTime::_split
172
* @return string The ISO 8601 formatted UTC date/time string.
174
function toUTC($datestr = null)
176
$regs = $this->_split($datestr);
179
return sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',
192
* Returns a unix timestamp.
194
* @param string|integer $datestr @see SOAP_Type_dateTime::_split
196
* @return integer The unix timestamp.
198
function toUnixtime($datestr = null)
200
$regs = $this->_split($datestr);
202
return strtotime(sprintf('%04d-%02d-%02d %02d:%02d:%02dZ',
214
* Compares two dates or this object with a second date.
216
* @param string|integer $date1 A unix timestamp or ISO 8601 date/time
218
* @param string|integer $date2 A unix timestamp or ISO 8601 date/time
219
* string. If empty, this object is used.
221
* @return integer The difference between the first and the second date.
223
function compare($date1, $date2 = null)
225
if (is_null($date2)) {
227
$date1 = $this->timestamp;
229
if (!is_int($date1)) {
230
$date1 = $this->toUnixtime($date1);
232
if (!is_int($date2)) {
233
$date2 = $this->toUnixtime($date2);
236
if ($date1 != -1 && $date2 != -1) {
237
return $date1 - $date2;