Search code examples
phpsql-serverdatesymfonydst

Symfony2 and PHP affected by DST, how do I stop this from affecting me?


In my Symfony 2 project we need to store a variety of information using dates and times. Currently in the database we are storing all of that information using date for the column type. And everything goes well when the information is stored for the first time in the database.

The issue is that, when I use the same form type for modifying the information, the data is altered after issuing a merge using Doctrine. Worse is, the database information, if I modify it without a frontend (using a UPDATE command), is stored without any issues, and it's displayed as it should be.

I have researched a bit on this issue, and it's because Symfony takes the server's timezone and applies it to the input data during the transformation from view to model. PHP usually considers DSTs, but because of political issues PHP is not taking the correct DST end date (it should have ended in March but it ended in April, and PHP takes the original March date) and thus the data gets altered correspondingly.

What should I do to avoid this problem from happening any further? In my country, DSTs have been somewhat variable in the last few years and I cannot trust my computer to take the correct date. I would like to make it timezone-agnostic.

  • How should I store my dates in the database?
  • How should I manage my dates in the Symfony project?

Thank you beforehand.


Solution

  • In order to have an immutable PHP timezone, always resort to UTC by doing this in php.ini:

    ;;;;;;;;;;;;;;;;;;;
    ; Module Settings ;
    ;;;;;;;;;;;;;;;;;;;
    
    ...
    
    [Date]
    ; Defines the default timezone used by the date functions
    ; http://php.net/date.timezone
    date.timezone=UTC
    

    This will force PHP to have a timezone that will never change despite any DST changes.