My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php

/**
* Eases handling dates/times in different timezones by allowing you to create
* a timezone context. This can be helpful when you need to, e.g., parse date
* strings created in a different timezone.
*
* Currently the class only provides strtotime() and date(), but this is usually
* sufficient.
*
* <code>
* // parse a date string from Eastern Standard Time (no DST)
* $tz = new MrClay_TimeZone(-5);
* $time = $tz->strtotime('2007-06-01 08:00:00');
*
* // display in New York time
* $tz = new MrClay_TimeZone('America/New_York');
* echo $tz->date('G', $time); // echoes '9'
* </code>
*/
class MrClay_TimeZone {

private $dtz = null;
private $time = null;
private $tz = null;

/**
* Create a "timezone shift" object.
*
* @param $tz timezone. This can either be a string as given by
* http://twiki.org/cgi-bin/xtra/tzdatepick.html or a GMT offset like -5.
* -5 would be converted to the string "Etc/GMT+5" for you.
*
* @param $time optional current timestamp in case you want to "set" time
*
* @return null
*/
public function __construct($tz, $time = null)
{
if (is_numeric($tz)) {
$this->tz = (0 == $tz)
? 'Etc/GMT'
: 'Etc/GMT' . ($tz > 0 ? '-' : '+') . abs($tz);
} else {
$this->tz = $tz;
}
$this->dtz = date_default_timezone_get();
$this->time = ($time !== null)
? $time
: (isset($_SERVER['REQUEST_TIME'])
? $_SERVER['REQUEST_TIME']
: time()
);
}

/**
* Format a "local" time/date according to the timezone given in the
* constructor
*
* <code>
* // display current New York time
* $tz = new MrClay_TimeZone('America/New_York');
* echo $tz->date('G:i:s');
* </code>
*
* @see http://www.php.net/manual/en/function.date.php
*
*/
public function date($format, $time = null)
{
if (null === $time) {
$time = $this->time;
}
$this->_shift();
return $this->_unshift(date($format, $time));
}

/**
* Parse about any English textual datetime description into a Unix
* timestamp. If given a date/time, it is parsed as if it is local to
* the timezone given in the constructor.
*
* <code>
* // parse a date string from Eastern Standard Time (no DST)
* $tz = new MrClay_TimeZone(-5);
* $time = $tz->strtotime('2007-06-01 08:00:00');
* </code>
*
* @see http://www.php.net/manual/en/function.strtotime.php
*/
public function strtotime($strTime, $now = null)
{
$this->_shift();
return $this->_unshift(strtotime($strTime, $now));
}

/**
* Set the default timezone to the user's request.
*/
private function _shift() {
date_default_timezone_set($this->tz);
}

/**
* Return the default timezone to the original value.
*
* $val is passed through avoiding the need for a temp variable in the
* calling function.
*/
private function _unshift($val) {
date_default_timezone_set($this->dtz);
return $val;
}

/* TEST THESE
public function getdate($time = null)
{
if (null === $time) {
$time = $this->time;
}
$this->shift();
return $this->_unshift(getdate($time));
}

public function localtime($time = null, $isAssoc = false)
{
if (null === $time) {
$time = $this->time;
}
$this->shift();
return $this->_unshift(localtime($time, $isAssoc));
}

// FIX
public function mktime($h = null, $m = null, $s = null, $mm = null, $dd = null, $yy = null)
{
$this->shift();
return $this->_unshift(mktime($h, $m, $s, $mm, $dd, $yy, idate('I')));
}

public function strftime($format, $time = null)
{
if (null === $time) {
$time = $this->time;
}
$this->shift();
return $this->_unshift(strftime($format, $time));
}

public function strptime($date, $format)
{
$this->shift();
return $this->_unshift(strptime($date, $format));
}

*/
}
Show details Hide details

Change log

r2 by st...@mrclay.org on Oct 10, 2008   Diff
Initial commit
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 4316 bytes, 157 lines
Hosted by Google Code