本文介绍了是否可以从偏移量创建DateTimeZone?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
构造函数仅接受区域名称:
new DateTimeZone('Europe / London');
而不是UTC的偏移量:
new DateTimeZone('+ 01:00'); //未知或不好的时区(+01:00)
然而,可以获得这样的来自的DateTimeZone:
(new DateTime('2012-12-28T00:00:00 + 01:00')) - > getTimezone() - > getName(); // +01:00
所以这有点怪异。有没有办法从偏移量直接获取DateTimeZone?
解决方案
除了,到目前为止我发现的最简单的方法是:
DateTime :: createFromFormat('O','+01:00') - > getTimezone();
修改 p>
这是一个这在PHP 5.5.10中已经修复。现在工作!
The DateTimeZone constructor only accepts a region name:
new DateTimeZone('Europe/London');
And not an offset from UTC:
new DateTimeZone('+01:00'); // Unknown or bad timezone (+01:00)
However, it is possible to obtain such a DateTimeZone from a DateTime:
(new DateTime('2012-12-28T00:00:00+01:00'))->getTimezone()->getName(); // +01:00
So this is a bit weird. Is there a way to directly obtain a DateTimeZone from an offset?
解决方案
In addition to Rafał's answer, the simplest way I've found so far is:
DateTime::createFromFormat('O', '+01:00')->getTimezone();
Edit
This was a bug that has been fixed in PHP 5.5.10. It now works!
这篇关于是否可以从偏移量创建DateTimeZone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!