简体   繁体   中英

Date Time zone issue with daylight saving

I have the following code:

var_dump(new \DateTime('now', new DateTimeZone('GMT')));

which i get the following output, which is one hour behind:

object(DateTime)#894 (3) { ["date"]=> string(19) "2012-09-13 13:54:26" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "UTC" } 

However if i change the code to the following:

var_dump(new \DateTime('now', new DateTimeZone(date_default_timezone_get())));

I then get the folling output which is right:

object(DateTime)#894 (3) { ["date"]=> string(19) "2012-09-13 14:54:26" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" } 

I was reading on Derick Rethans blog that with timezone 3 it should take into account, any daylight saving hours, can anyone explain why on the first code snippet I get one hour before what it actually is?!

Well, in the first snippet you're asking for a time zone of "GMT". That's a somewhat ambiguous term, but I'd normally expect that to mean exactly GMT itself - roughly equivalent to UTC, and without any daylight saving time. That's why it's not applying daylight saving time, and it's reporting the time zone as UTC.

The Europe/London time zone spends half the year in GMT, and half the year in BST.

I would suggest that you steer clear of 3-letter time zone abbreviations wherever possible. They're full of all kinds of possibilities for failure. (Having now read the blog post, that's basically what's being suggested there too.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM