Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Doctrine2 DateTime in UTC Only

There are similar questions, but I couldn't find any pertaining to Doctrine2 configuration, so here goes...

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

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.

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.

So the Doctrine docs show this example:

class UTCDateTimeType extends DateTimeType
{
// ...
}

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?

like image 652
Matt Avatar asked Jun 08 '12 20:06

Matt


1 Answers

I just found this part of a post about the Timestampable behavior that seems like a simpler solution:

config.yml

doctrine:
    dbal:
        types: 
            datetime: Acme\DoctrineExtensions\DBAL\Types\UTCDateTimeType
like image 189
Matt Avatar answered Oct 01 '22 07:10

Matt