My favorites
▼
|
Sign in
mrclay
Steve Clay's classes, functions, snippets, and whatzits
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
php
/
MrClay
/
TimeZone.php
‹r2
r84
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
158
159
160
<?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>
*
* @author Steve Clay <steve@mrclay.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
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
r83
by mrclay.org on Feb 28, 2012
Diff
Add license. Some needed cleanup
Go to:
/trunk/php/Coewp/JsonApi.php
/trunk/php/Coewp/MenuFilter.php
...hp/Coewp/ShortCode/Accordion.php
...ShortCode/Accordion/Renderer.php
...oewp/ShortCode/PageAccordion.php
/trunk/php/MrClay/AutoP.php
/trunk/php/MrClay/Bench.php
...nk/php/MrClay/CachedFunction.php
...ay/CachedFunction/Cache/File.php
/trunk/php/MrClay/Cli.php
/trunk/php/MrClay/Cli/Arg.php
.../php/MrClay/Crypt/ByteString.php
...php/MrClay/Crypt/Cipher/Base.php
...lay/Crypt/Cipher/Rijndael256.php
...k/php/MrClay/Crypt/Container.php
...MrClay/Crypt/Encoding/Base64.php
...lay/Crypt/Encoding/Base64Url.php
...t/Encoding/EncodingInterface.php
.../php/MrClay/Crypt/Encryption.php
.../php/MrClay/Crypt/KeyDeriver.php
.../MrClay/Crypt/PasswordHasher.php
...p/MrClay/Crypt/SignedRequest.php
/trunk/php/MrClay/FireLog.php
.../php/MrClay/FireLog/Response.php
...nk/php/MrClay/FireLog/Writer.php
/trunk/php/MrClay/HashUtils.php
...hp/MrClay/Hmac/SignedRequest.php
/trunk/php/MrClay/Html.php
/trunk/php/MrClay/JsonFormatter.php
/trunk/php/MrClay/LinkHelper.php
...lay/LinkHelper/LinkOrWrapper.php
...p/MrClay/LinkHelper/ListItem.php
...MrClay/LinkHelper/OpenAnchor.php
/trunk/php/MrClay/LiveOutput.php
.../MrClay/LiveOutput/Processor.php
...p/MrClay/LiveOutput/Renderer.php
/trunk/php/MrClay/Loader.php
/trunk/php/MrClay/Minitest.php
.../MrClay/NewPasswordValidator.php
/trunk/php/MrClay/QAD/App.php
...p/MrClay/QAD/ErrorController.php
/trunk/php/MrClay/QAD/View.php
...ay/QAD/View/CallbackResolver.php
/trunk/php/MrClay/Replay.php
/trunk/php/MrClay/RootFinder.php
...hp/MrClay/RootFinder/WetBulb.php
...nk/php/MrClay/SendmailFilter.php
/trunk/php/MrClay/StaticWrapper.php
/trunk/php/MrClay/StringDebug.php
/trunk/php/MrClay/Template.php
Project members,
sign in
to write a code review
Older revisions
r2
by st...@mrclay.org on Oct 10, 2008
Diff
Initial commit
All revisions of this file
File info
Size: 4436 bytes, 160 lines
View raw file
Powered by
Google Project Hosting