DateTime对象上的不同timezone_types [英] Different timezone_types on DateTime object

查看:672
本文介绍了DateTime对象上的不同timezone_types的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



created_at datetimetz:

  DateTime对象

[date] => 2013-04-18 11:54:34
[timezone_type] => 1
[时区] => +02:00

birthdate date:

  DateTime对象

[date] => 1970-01-01 00:00:00
[timezone_type] => 3
[时区] => ;欧洲/柏林

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



我该如何实现?

解决方案

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




  • 类型1;一个UTC偏移量,例如 new DateTime(2013年7月17日-0300);

  • 类型2;时区缩写,例如 new DateTime(2013年7月17日格林尼治标准时间);

  • 类型3:时区标识符,如 new DateTime(2013年7月17日,新的DateTimeZone(Europe / London));



只有附有类型3时区的DateTime对象才能正确地使用DST。



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


I use Doctrine2 on Postgres. In one table I have got two different date types: birthdate:date and created_at:datetimetz. Both become DateTime object but with different timezone_type. Here are listings:

created_at datetimetz:

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

birthdate date:

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

I need to format my objects in the same way. Both should have timezone_type=3.

How can I achieve that?

解决方案

Timezones can be one of three different types in DateTime objects:

  • Type 1; A UTC offset, such as in new DateTime("17 July 2013 -0300");
  • Type 2; A timezone abbreviation, such as in new DateTime("17 July 2013 GMT");
  • Type 3: A timezone identifier, such as in new DateTime( "17 July 2013", new DateTimeZone("Europe/London"));

Only DateTime objects with type 3 timezones attached will allow for DST correctly.

In order to always have type 3 you will need to store the timezone in your database as accepted identifiers from this list and apply it to your DateTime object on instantiation.

这篇关于DateTime对象上的不同timezone_types的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆