5

DateTimeZone构造函数只接受一个区域名称:

new DateTimeZone('Europe/London');

而不是与 UTC 的偏移:

new DateTimeZone('+01:00'); // Unknown or bad timezone (+01:00)

但是,可以从 DateTime 获得这样的DateTimeZone

(new DateTime('2012-12-28T00:00:00+01:00'))->getTimezone()->getName(); // +01:00

所以这有点奇怪。有没有办法直接从偏移量获取 DateTimeZone ?

4

4 回答 4

5

除了Rafał 的回答之外,到目前为止我发现的最简单的方法是:

DateTime::createFromFormat('O', '+01:00')->getTimezone();

编辑

这是一个已在 PHP 5.5.10 中修复的错误。现在可以了!

于 2012-12-28T11:47:39.343 回答
2

现代答案:

new DateTimeZone('+0100');

文档:

http://php.net/manual/en/datetimezone.construct.php

于 2017-12-04T23:09:12.777 回答
1

看看这个功能。

http://pl1.php.net/manual/en/function.timezone-name-from-abbr.php

您需要将小时转换为秒并将它们作为第二个参数传递。

某事。喜欢new DateTimeZone(timezone_name_from_abbr('', 3600, 0))应该工作。

于 2012-12-28T11:22:55.250 回答
0

我认为您没有任何预定义的方式。但是,如果您声明任何将返回日期和时间并添加偏移量的全局函数,它可能会对您有所帮助。例子 :

function getDateTime($format="dd-mm-YY"){

$currDate= date($format);

$currDate=date($format,strtotime("+1 day",$currDate); // or whatever needed instead of +1 day

}
于 2012-12-28T11:30:47.843 回答