仅在UTC中存储Doctrine2 DateTime [英] Storing Doctrine2 DateTime in UTC Only
问题描述
有类似的问题,但是我找不到任何有关Doctrine2配置的信息,所以这里是...
There are similar questions, but I couldn't find any pertaining to Doctrine2 configuration, so here goes...
我已经阅读了这个页面描述时区对于Doctrine2,我只是想澄清,因为不清楚如何实际使用这个: http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html
I've read over this page describing timezones for Doctrine2, and I just want to clarify because it isn't clear how to actually use this: http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html
我想以UTC为单位存储数据时间,以便每个用户可以根据其位置拥有关联的时区,但数据是一般存储的。
I want to store datetimes in UTC so that each user can have an associated timezone based on their location but the data is stored generically.
基本上,数据是商店的营业时间(例如上午9点开放,下午6点关闭)。商店将跟踪其时区,因此当商店中的某人设置打开时间时,他们正在处理商店的时区,时间被转换为UTC并存储在数据库中。接下来,当不同的用户查看商店的时间时,他们将看到他们自己的时区或商店时区的开放时间。不要紧,因为我有时间在UTC,所以我可以将它们转换成任何时区。
Basically, the data is the hours for a store (open at 9AM and close at 6PM for example). The store will keep track of its timezone so when a person at a store sets the "open" time, they are dealing with the store's timezone, the time gets converted to UTC and stored in the database. Next when a different user views the stores hours, they will see the open time in either their own timezone or the store timezone. It shouldn't matter because I have the times in UTC, so I can convert them to any timezone.
所以Doctrine文档显示了这个例子:
So the Doctrine docs show this example:
class UTCDateTimeType extends DateTimeType
{
// ...
}
但它并没有说明如何使用它。我必须声明列为UTCDateTime而不是DateTime?这个课程需要在哪里生活,以便教义知道该类型存在?
But it doesn't say how to use it. Would I have to declare columns as "UTCDateTime" instead of "DateTime"? Where does this class need to live so that Doctrine is aware that the type exists?
推荐答案
我刚刚发现这部分帖子关于Timestampable行为似乎是一个更简单的解决方案:
I just found this part of a post about the Timestampable behavior that seems like a simpler solution:
config.yml
config.yml
doctrine:
dbal:
types:
datetime: Acme\DoctrineExtensions\DBAL\Types\UTCDateTimeType
这篇关于仅在UTC中存储Doctrine2 DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!