DateTime对象不受其时间戳约束吗? [英] DateTime object not bound by its timestamp?
问题描述
DateTime
对象不受其时间戳约束吗?还是 getTimestamp()有某种副作用> 何时用于DST更改?
详细信息
当设置时间戳记时 DST
上的 DateTime
对象(表示格式化的时间在更改时钟之前/之后都存在)返回的时间戳
$ php --version
PHP 7.1.3(cli)(内置:Mar 17 2017 16:59:59)(NTS)
版权所有(c)1997-2017 The PHP Group
Zend Engine v3.1.0,版权所有(c)1998-2017 Zend Technologies
复制
考虑以下 php
脚本:
date_default_timezone_set('Europe / Berlin');
$ date =新的DateTime();
$ set_timestamp = 1319932800;
$ date-> setTimestamp($ set_timestamp);
$ get_timestamp = $ date-> getTimestamp();
fwrite(STDERR,$ set_timestamp。 \n); // 1319932800
fwrite(STDERR,$ get_timestamp。 \n); // 1319936400 **(为什么如此?)**
为什么打印的值不相等吗?
首先,unix时间戳始终在UTC,因此它没有时区和夏令时。
另一方面, DateTime
对象存储本地时间仅(本地表示在 DateTime
实例中设置的时区)。
因此,在设置时间戳之前,应将时区设置为 +00:00 或 UTC ,以避免不必要的时间转换和DST猜测。 p>
您有两个选择:
1。通过 DateTime
的构造函数设置时间戳构造函数将覆盖默认时区并将其显式设置为 +00 :00 ,当它在第一个参数中获得时间戳(以 @
开头)时:
$ set_timestamp = 1319932800;
$ date =新的DateTime('@'。$ set_timestamp);
print($ set_timestamp。 \n);
print($ date-> getTimestamp()。 \n);
信息:在这种情况下,构造函数的时区参数将始终被覆盖。
2。在调用 setTimestamp()
之前设置时区调用 setTimezone()
具有 DateTimeZone('+ 00:00')
或 DateTimeZone('UTC')
时区,然后调用 setTimestamp()
:
$ set_timestamp = 1319932800;
$ date = new DateTime();
$ date-> setTimezone(new DateTimeZone(’UTC’));
$ date-> setTimestamp($ set_timestamp);
print($ set_timestamp。 \n);
print($ date-> getTimestamp()。 \n);
注释
当然,两者在这些情况下,输出将是:
1319932800
1319932800
在这些情况下, date_default_timezone_set()
是不必要的,因为您不想用本地时间做任何事情。
但是当您要打印 $ date
时以人类可读的格式(因此,当您将unix时间戳转换为本地时间)时区将再次变得有趣。
Is a DateTime
object not bound by its timestamp? Or does getTimestamp() has some kind of side-effect when used on DST change?
Details
When setting the timestamp of a DateTime
object which is on DST
(meaning the formatted time exists both before/after changing the clock) the returned timestamp differs from the set timestamp.
$ php --version
PHP 7.1.3 (cli) (built: Mar 17 2017 16:59:59) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
Reproduce
Consider the following php
script:
date_default_timezone_set('Europe/Berlin');
$date = new DateTime();
$set_timestamp = 1319932800;
$date->setTimestamp($set_timestamp);
$get_timestamp = $date->getTimestamp();
fwrite(STDERR, $set_timestamp . "\n"); // 1319932800
fwrite(STDERR, $get_timestamp . "\n"); // 1319936400 **(WHY IS THIS DIFFERENT?)**
Why are the printed values not equal?
First of all, the unix timestamp is always in UTC, so it hasn't timezone and DST.
In other hand, the DateTime
object stores local time only (the "local" means what timezone is set in the DateTime
instance).
Therefore you should set timezone to +00:00 or UTC before you set a timestamp for avoid unnecessary time conversions and DST guessing.
You have two choices:
1. Set timestamp via constructor of DateTime
The constructor will overrides the default timezone and explicit set to +00:00 when it got timestamp (started with @
) in first parameter:
$set_timestamp = 1319932800;
$date = new DateTime('@' . $set_timestamp);
print($set_timestamp . "\n");
print($date->getTimestamp() . "\n");
Info: in this case, the timezone parameter of constructor always will be overridden.
2. Set timezone before call setTimestamp()
Call setTimezone()
with DateTimeZone('+00:00')
or DateTimeZone('UTC')
timezone before you call setTimestamp()
:
$set_timestamp = 1319932800;
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp($set_timestamp);
print($set_timestamp . "\n");
print($date->getTimestamp() . "\n");
Notes
Of course, both of these cases the output will be:
1319932800
1319932800
The date_default_timezone_set()
is unnecessary in these cases, because you don't want to do anything with local time.
However when you want to print the $date
in human readable format (so when you convert the unix timestamp to local time) the timezone will be interesting again.
这篇关于DateTime对象不受其时间戳约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!