不使用UTC显示时区

时间:2009-10-13 17:42:52

标签: php datetime timezone

我一直在阅读如何在整个上午这样做,并且普遍的共识是:将您的时间戳存储在UTC中并从那里计算偏移量。

然而,像大多数人一样,我维护现有的代码库,但我无法改变存储日期的方式。至少,不是在规定的时间范围内。

所以我的问题是,我可以安全地做这样的事吗?

// this is my time zone
$timestamp = strtotime($timestampFromDatabase);
date_default_timezone_set('America/New York');
$tz = date_default_timezone_get();

// this is theoretically their timezone, which will eventually
// be taken from a session variable
date_default_timezone_set('Europe/Paris');
$offset = time() - $timestamp;
$timestamp -= $offset;

// now that I have the offset, display dates like this
date('r', $timestamp);

有些人建议不要做日期算术,但我在这里做错了什么?有没有更好的办法?是否有需要注意的陷阱?我特别感兴趣的是这种方法会产生什么样的错误,如果有的话,他们会影响谁?

我的老板是那种不关心这个bug只影响1%用户群的人。

编辑:我能说清楚吗?我似乎没有太多的接受者。任何人?即使是模糊的建议或链接也会非常有用。

1 个答案:

答案 0 :(得分:5)

简短的回答是时区很难处理,没有人愿意解决困难的问题!

时间越久,答案就是:

首先,您需要在当前时区创建日期的表示。以下是等效的:

date_default_timezone_set('America/New York');

$date = new DateTime(null);

OR

$date = new DateTime(null, new DateTimeZone('America/New York'));   

这会为您提供一个区域设置为America / New York的日期。我们可以轻松地将其转换为用户时区,在本例中为欧洲/巴黎,下一行:

$date->setTimezone(new DateTimeZone('Europe/London'));

您可以使用以下行来获取在任何时候表示的日期/时间。

echo $date->format('d/m/Y H:i:s');

强烈建议使用算术,因为内置的PHP函数知道夏令时和许多复杂的事情你不会正确。


我的测试脚本完整:

date_default_timezone_set('America/Belize');

$date = new DateTime(null);

echo $date->format('d/m/Y H:i:s') . '<br>';

$date->setTimezone(new DateTimeZone('Europe/London'));

echo $date->format('d/m/Y H:i:s') . '<br>';