PHP DateTime DST [英] PHP DateTime DST
问题描述
我也搜索了PHP手册和StackOverflow,但是并没有真正找到我的问题的正确答案。我的日期以UTC存储。现在如果我这样做:
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');
DateTime :: setTimezone()会自动设置DST吗?那么如果我格式化字符串,那么夏天的时间是15:00:00,冬天的时间是16:00:00吗?或者我必须手动设置DST?
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?
相关问题,如果我想得到一个独立的UTC时间(我可以存储),以下工作? / p>
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');
或者更好地使用简单的 gmdate('Ymd H:i:s' )
?
Or better to use simple gmdate('Y-m-d H:i:s')
?
推荐答案
您的假设是正确的。
此外,这将花费你10秒钟来验证自己。
In addition, this would have taken you 10 seconds to verify yourself.
编辑:
正确的语法是:
$date = new DateTime('2012-03-16 14:00:00', new DateTimeZone('UTC'));
关于你的第二个问题。在DateTime构造函数中传递的时区是'reference'时区。
As to your second question. The timezone passed in the DateTime constructor is the 'reference' timezone.
你还应该调用 - > setTimeZone(new DateTimeZone('UTC'))来格式化UTC时区,除非默认时区也设置为UTC(date_default_timezone_set)。
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.
这篇关于PHP DateTime DST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!