代码块应该能够为自己说话。时区Europe/Berlin
被检测为+02:00
,但行为类似于+01:00
。当我专门使用+02:00
作为时区时,它会按预期工作。谁能向我解释一下这里发生了什么?
>>> $timezone = new DateTimeZone('Europe/Berlin')
=> DateTimeZone {#2356
timezone: Europe/Berlin (+02:00),
}
>>> $timestamp = DateTime::createFromFormat('Y-m-dTH:i:s.uO', '2020-02-29T07:30:00.000+0100')
=> DateTime @1582957800 {#2349
date: 2020-02-29 07:30:00.0 +01:00,
}
>>> $timestamp->setTimezone($timezone)
=> DateTime @1582957800 {#2349
date: 2020-02-29 07:30:00.0 Europe/Berlin (+01:00),
}
>>> $timezone = new DateTimeZone('+02:00')
=> DateTimeZone {#2371
timezone: +02:00,
}
>>> $timestamp = DateTime::createFromFormat('Y-m-dTH:i:s.uO', '2020-02-29T07:30:00.000+0100')
=> DateTime @1582957800 {#2364
date: 2020-02-29 07:30:00.0 +01:00,
}
>>> $timestamp->setTimezone($timezone)
=> DateTime @1582957800 {#2364
date: 2020-02-29 08:30:00.0 +02:00,
}
柏林在标准时间(英语中称为中欧时间或 CET)使用 UTC 偏移量+01:00
,在夏令时(英语中称为中欧夏令时或 CEST)使用+02:00
偏移量。 参考这里。
在第一个示例中,您将柏林时间应用于2020-02-29
CET 生效的时间,而不是 CEST。 因此,您会在结果中看到+01:00
偏移量。 这是正确的。
在第二个示例中,您不是应用柏林时间,而是应用具有固定偏移量+02:00
的时区,因此您可以在结果中看到这一点。
另请参阅时区标签维基中标题为"时区!=偏移量"的部分。