Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...