Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

date - PHP DateTime DST

I searched the PHP manual and StackOverflow too, but haven't really found a proper answer to my question. My dates are stored in UTC. Now if I do:

$date = new DateTime('2012-03-16 14:00:00', 'UTC');
$date->setTimezone('Europe/Budapest');

Will DateTime::setTimezone() set DST automatically? So if I format the string, will it output 15:00:00 in summer time and 16:00:00 in winter time? Or will I have to set DST manually?

Related question, if I want to get a DST independent UTC time (that I can store), will the following work?

$date = new DateTime('now', 'UTC');
$date->format('Y-m-d H:i:s');

Or better to use simple gmdate('Y-m-d H:i:s')?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your assumption is correct.

In addition, this would have taken you 10 seconds to verify yourself.

Edit:

The correct syntax is:

$date = new DateTime('2012-03-16 14:00:00', new DateTimeZone('UTC'));

As to your second question. The timezone passed in the DateTime constructor is the 'reference' timezone.

You should still call ->setTimeZone(new DateTimeZone('UTC')) to format it in the UTC timezone, unless the default timezone was also set to UTC (date_default_timezone_set).

Next time, try a little harder trying stuff out before asking.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...