我在 Postgres 上使用 Doctrine2。在一张表中,我有两种不同的日期类型:birthdate:date
和 created_at:datetimetz
。两者都成为 DateTime 对象,但具有不同的 timezone_type
。
以下是 list :
created_at
datetimetz:
DateTime Object
(
[date] => 2013-04-18 11:54:34
[timezone_type] => 1
[timezone] => +02:00
)
生日
日期:
DateTime Object
(
[date] => 1970-01-01 00:00:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
我需要以同样的方式格式化我的对象。两者都应该有 timezone_type=3
。
我怎样才能做到这一点?
最佳答案
时区可以是 DateTime 对象中的三种不同类型之一:
- 类型 1; UTC 偏移量,例如
new DateTime("17 July 2013 -0300");
- 类型 2;时区缩写,例如
new DateTime("17 July 2013 GMT");
- 类型 3:时区标识符,例如
new DateTime("17 July 2013", new DateTimeZone("Europe/London"));
只有附加了类型 3 时区的 DateTime 对象才能正确使用 DST。
为了始终使用类型 3,您需要将时区存储在数据库中,作为来自 this list 的接受标识符并将其应用于实例化的 DateTime 对象。
关于php - DateTime 对象上的不同 timezone_types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17694894/