我无法理解为什么使用date()函数和DateTime对象会得到不同的结果。我在Mac上。
date_default_timezone_set('Europe/Sofia');
echo date('Y-m-d h:i:s'); // 2015-04-02 01:18:59 correct
$date = new DateTime('@'.time());
echo $date->format('Y-m-d h:i:s'); // 2015-04-01 10:18:59 offset
编辑
已尝试$date = new DateTime('@'.time(), new DateTimeZone('Europe/Sofia'));
无影响
date_default_timezone_set()
不会影响DateTime
类,因此您必须使用DateTime
中的方法进行设置,如下所示:
$date = new DateTime("@".time());
$date->setTimezone(new DateTimeZone('Europe/Sofia'));
echo $date->format('Y-m-d h:i:s') . "<br>";
旁注:
通常你也可以这样做:
$date = new DateTime("@".time(), new DateTimeZone('Europe/Sofia'));
但由于您使用了时间戳,出于某种原因,这是不可能的。
这已经在错误跟踪器中了:https://bugs.php.net/bug.php?id=40743