DateTime对象上的timezone_types不同

lil*_*lly 29 php postgresql timezone datetime

我在Postgres上使用Doctrine2.在一个表中,我有两种不同的日期类型:birthdate:datecreated_at:datetimetz.两者都成为DateTime对象但具有不同timezone_type.这是列表:

created_at datetimetz:

DateTime Object
(
    [date] => 2013-04-18 11:54:34
    [timezone_type] => 1
    [timezone] => +02:00
)
Run Code Online (Sandbox Code Playgroud)

birthdate 日期:

DateTime Object
(
    [date] => 1970-01-01 00:00:00
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)
Run Code Online (Sandbox Code Playgroud)

我需要以相同的方式格式化我的对象.两者都应该有timezone_type=3.

我怎样才能做到这一点?

vas*_*ite 68

时区可以是DateTime对象中三种不同类型之一:

  • 类型1; UTC偏移量,例如innew DateTime("17 July 2013 -0300");
  • 2型; 时区缩写,例如innew DateTime("17 July 2013 GMT");
  • 类型3:时区标识符,例如in new DateTime( "17 July 2013", new DateTimeZone("Europe/London"));

只有附加了类型3时区的DateTime对象才能正确使用DST.

为了始终具有类型3,您需要将数据库中的时区存储为此列表中的可接受标识符,并在实例化时将其应用于DateTime对象.

  • 我发现在另一个ORM上也有同样的问题,即`$ new = new DateTime();。$ new-> setTimestamp($ old-> getTimestamp());尽管很丑陋,但是可以达到目的。将其包含在二传手中可以减轻痛苦。 (2认同)