How do I get Greenwich Mean Time in PHP?

后端 未结 6 1263
情深已故
情深已故 2020-11-29 00:33

I have a server which is set to EST, and all records in the database are set to EST. I would like to know how to set it to GMT. I want to offer a time zone option to my user

6条回答
  •  不知归路
    2020-11-29 01:08

    It is important to note that if you are using date('Z') to convert a date that is stored in a database that you give the date the timestamp as the second argument, otherwise your result will not be accurate.

    e.g.

    In the UK, sometimes date('Z') will give 3600, sometimes it will give 0.

    echo date('Z', strtotime('2012-01-01'));
    

    Gives 0

    echo date('Z', strtotime('2012-07-01'));
    

    Gives 3600

    So purely using strtotime($dbDateValue) - date('Z') can be wrong

    Instead use:

    $dbDateTimestamp = strtotime($dbDateValue); // where $dbDateValue something like 2012-01-01 22:34:00
    
    $utcTimestamp = strtotime($dbDateTimestamp) - date('Z', $dbDateTimestamp);
    

    This of course relies on PHP and the database both being set to the same timezone.

提交回复
热议问题