php DateTime 对象上的不同 timezone_types

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17694894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:16:40  来源:igfitidea点击:

Different timezone_types on DateTime object

phppostgresqldatetimetimezone

提问by lilly

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

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

created_atdatetimetz:

created_at日期时间:

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

birthdatedate:

birthdate日期:

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.

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

How can I achieve that?

我怎样才能做到这一点?

回答by vascowhite

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

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

  • 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"));
  • 类型 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"));

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

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

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

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

回答by alwayslearning

I know this is an ancient post, but since Doctrine was mentioned, I feel the need to share what I've recently experienced regarding this issue.

我知道这是一个古老的帖子,但是自从提到了 Doctrine,我觉得有必要分享我最近在这个问题上的经历。

@vascowhite is correct, but regarding Doctrine (at least on my configuration) dates sent to the server (e.g. to save) via HTTP are converted timezone type 2. Doctrine handles them properly and saves the date correctly, but it does notconvert the timezone type.

@vascowhite是正确的,但对于原则(至少在我的配置)发送到服务器的日期(如保存)通过HTTP转换时区类型2.学说处理它们妥善和正确保存日期,但它并不会转换时区类型。

If you are performing a "save and and return fresh values" type operation, note that Doctrine will use the cached value (with timezone_type 2). This becomes important when you are expecting timezone_type 3 as you would normally get during a page reload. We have a custom date converter JS class that expects timezone_type 3 and it was unable to convert it. Admittedly, the date converter should account for this, but also one should be aware that the timezone type will be the last loaded value in Doctrine. Clearing Doctrine's cache after saving will force the data to reload with timezone_type 3.

如果您正在执行“保存并返回新值”类型的操作,请注意 Doctrine 将使用缓存值(使用 timezone_type 2)。当您期望 timezone_type 3 时,这变得很重要,因为您通常会在页面重新加载期间获得。我们有一个自定义日期转换器 JS 类,它需要 timezone_type 3 并且无法转换它。诚然,日期转换器应该考虑到这一点,但也应该注意时区类型将是 Doctrine 中最后加载的值。保存后清除 Doctrine 的缓存将强制使用 timezone_type 3 重新加载数据。