ka_*_*lin 0 php datetime datetime-format
这是我在 timezone 和 DateTime 中遇到的一个奇怪的问题,我会保持简短:
var_dump($obj->getTime1());
var_dump($obj->getTime2());
Run Code Online (Sandbox Code Playgroud)
给我以下信息(没关系,时区很好等等):
object(DateTime)#379 (3) {
["date"]=>
string(19) "2016-03-28 19:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(12) "Europe/Paris"
}
object(DateTime)#378 (3) {
["date"]=>
string(19) "2016-03-13 19:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(12) "Europe/Paris"
}
Run Code Online (Sandbox Code Playgroud)
但是当我想将(在完全相同的行上)格式化为 string 时:
var_dump($obj->getTime1()->format(DateTime::ISO8601));
var_dump($obj->getTime2()->format(DateTime::ISO8601));
Run Code Online (Sandbox Code Playgroud)
我得到:
string(24) "2016-03-28T19:00:00+0200" <-- Noticed that it has a different timezone
string(24) "2016-03-13T19:00:00+0100"
Run Code Online (Sandbox Code Playgroud)
我讨厌日期时间
PS:当我创建第二个 DateTime 对象时,我克隆了第一个对象并添加了一个 DateInterval:
$someTime = clone $obj->getTime1();
$someTime ->sub(new \DateInterval('P'.$days.'D'));
$event->setTime2($someTime);
Run Code Online (Sandbox Code Playgroud)
您多出一个小时的原因是因为在 3 月的最后一个(完整)周末,我们“更改”了时区,并跳过了一个小时。
今年,这发生在 3 月 27 日,这意味着我们实际上是在另一个时区。DateTime 对象会自动为您执行此操作,这就是您拥有 2 个不同时区的原因。
它将与 10 月 30 日的 10 月最后一个(完整)周末相反。
| 归档时间: |
|
| 查看次数: |
130 次 |
| 最近记录: |