Doctrine DBAL
  1. Doctrine DBAL
  2. DBAL-921

[GH-616] Always store dates in UTC


    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Security Level: All
    • Labels:


      This issue is created automatically through a Github pull request on behalf of LinusU:



      This PQ will make sure that the dates saved in the database (without indication of timezone) is always stored in the UTC timezone.

      I was doing development on my machine in Sweden when I noticed that when I created a `DateTime`, stored it in the db and then retrieved it again, the time was off by two hours. This is because a created the `DateTime` object with the UTC timezone. Doctrine then saved it straight to the database (by using `$date->format(...)`) and thus the information about which timezone it was in was lost. When doctrine then fetched the value, it used `DateTime::createFromFormat(...)` to create a `DateTime` for me. The problem is that since the timezone wasn't saved anywhere, it assumed that it was a Swedish date, and thus it removed two hours.

      I believe that the correct way of doing it is to store the dates in the db as UTC. Then it will always work no matter what the default timezone is, even if I later decide to change it.

      `date_default_timezone_set('UTC')` is not the answer. If I use it, I need to make sure that every `DateTime` that I pass to doctrine always has the timezone set to `UTC`. Since the `DateTime` can come from any number of sources (e.g. third party library) it could easily introduce hard to detect bugs. It will also output every date in the UTC timezone which may not be what I want if I'm developing a localised site (e.g. small page for a local Swedish business).


        Doctrine Bot added a comment -

        A related Github Pull-Request [GH-616] was closed:

        Doctrine Bot added a comment - A related Github Pull-Request [GH-616] was closed:


          • Assignee:
            Benjamin Eberlei
            Doctrine Bot
          • Votes:
            0 Vote for this issue
            1 Start watching this issue


            • Created: