To GMT or not to GMT
A leap day with a post on Date/Time issues seems fitting...
Earlier today, on twitter, @skoop asked: "dear #lazyweb, when I use DateTimeZone('GMT'), why does format('e') output UTC?" What he means is that:
$date = new DateTime('now', new DateTimeZone('GMT'));
echo $date->format(DateTime::RFC2822 . ' e' );
which shows:
Wed, 29 Feb 2012 16:26:23 +0000 UTC
As you can see that has UTC and not GMT as you might expect.
If you look closely at the documentation for the "Other" group of timezones, it lists with the GMT timezone as warning: "Please do not use any of the timezones listed here (besides UTC), they only exist for backward compatible reasons." If you use GMT as timezone identifier in the constructor to DateTimeZone, PHP will instead use the correct UTC in output. When you create a DateTimeZone object like this, you will always get a "type 3" DateTimeZone object:
$date = new DateTime('now', new DateTimeZone('GMT'));
var_dump($date);
which shows:
object(DateTime)#1 (3) {
["date"]=>
string(19) "2012-02-29 16:30:51"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
Now apparently some systems *cough*Silverlight*cough* require GMT to be used. GMT is not a timezone, but just a timezone abbreviation meant for output only. Read more about that in the article "Leap Seconds and What To Do With Them". However, if it is necessary you can create a DateTime object with a different timezone type. In this case you want a "type 2" timezone associated with the DateTime object. You do that by simply forcing that timezone abbreviation when instantiating a DateTime object:
$date = new DateTime('today GMT');
var_dump( $date );
which shows:
object(DateTime)#1 (3) {
["date"]=>
string(19) "2012-02-29 16:32:16"
["timezone_type"]=>
int(2)
["timezone"]=>
string(3) "GMT"
}
Things like this also work:
$date = new DateTime( "GMT" ); $date->setDate( 2012, 2, 19 ); var_dump( $date );
And of course, this is not limited to GMT only:
$date = new DateTime( "EST" ); $date->setDate( 2012, 2, 19 ); var_dump( $date );
which shows:
object(DateTime)#1 (3) {
["date"]=>
string(19) "2012-02-19 16:37:58"
["timezone_type"]=>
int(2)
["timezone"]=>
string(3) "EST"
}
As a reminder of the three different types of timezones that can be attached to DateTime objects:
-
A UTC offset, such as in
new DateTime( "2012-02-29 -0500" ); -
A timezone abbreviation, such as in
new DateTime( "2012-02-29 EST" ); -
A timezone identifier, such as in
new DateTime( "2012-02-29 America/Montreal" );
Please also be aware that only DateTime objects with "type 3" timezones attached to them will calculate correctly over Daylight Saving Time boundaries.
If you want to learn more about Dates and Times, and how to use them with PHP, please get a copy of my book "php|architect's Guide to Date and Time Programming".
Comments
Varnish seems to be picky about the format of the If-Modified-Since/Last-Modified request/response headers if it is to support 304 Not Modified.
On some projects I have had to create a GMT timestamp by doing something like this to make Varnish respond with a 304:
$date = new DateTime('@' . $someUnixTimestamp);
header('Last-Modified: ' . $date->format('D, d M Y H:i:s') . ' GMT');
An example of the formatted timestamp:
Mon, 27 Feb 2012 12:31:48 GMT
Where's the +1 button ?
Thanks Derick for this !
Life Line
I've finished reading This Way Up. It's about maps, that went wrong.
It's a good read, but htyerr were several chapters that were written in a novel way (as a video transcript, a series of letters), and I found distracting from the a tail content. It'll have worked better in a produced video.
No mention of @openstreetmap though :-(
Updated a bench
Created a tree; Updated 3 humps and a waste_basket
The Early Cormorant Catches the Eel
Sorry, not the best photo! But I caught this Cormorant catching this large eel when looking for Bank Swallows, right next to Eel Pie Island in the Thames.
#Birds #BirdPhotography #BirdsOfMastodon #Photography #London
Updated an estate_agent office
I went to my nieces' birthday party yesterday.
The theme was pink, and that included all the food, mostly died with beet root.
Shock and horror this morning when doing number two. Not only was my turd dark red, it was also glittering at me. Apparently the carrot cake had edible glitter...
So now I know what's worse than glitter.
😂 ✨ 💩 🟣Long-Tailed Tit on a Branch with Lichen
I've been spending some time in random London local nature reserves.
Sitting and listening, and in fifteen minutes you spot countless species.
This one was in Ham Lands Local Nature Reserve near Teddington.
#london #BirdPhotogaphy #BirdsOfMastodon #Birds #LichenSubscribe
A Colourful Mandarin
In The Long Water in Kensington Palace Gardens, London.
Created 7 benches
Created 2 benches
Created a bench
I walked 7.3km in 2h28m39s
Added a note about a duplicate Papersmiths
I walked 4.1km in 49m02s
Fixed website
fix typo
Updated a bench
I walked 1.6km in 20m26s
I walked 1.1km in 11m49s
The Yellow Eye
A blue heron's head, with its very yellow stare-y eye.
#BirdPhotography #Photography #BirdsOfFediverse #BirdsOfMastodon #London
My little Lego box is telling me it really is quite warm outside.
Created a bicycle_parking and a crossing
I walked 3.3km in 41m56s








Shortlink
This article has a short URL available: https://drck.me/gmt-9bx