Everywhere I read about converting time to a user's timezone says that the best method is to store a date and time in UTC then just add the user's timezone offset to this time.
How can I store a date in UTC time? I use the MySQL DATETIME field.
When adding a new record to MySQL in my PHP code I would use now()
to insert into MySQL DATETIME.
Would I need to use something different than now()
to store UTC time?
I would suggest inserting the date in UTC time zone. This will save you a lot of headaches in the future with daylight saving problems.
When I query my data I use the following PHP script:
You can replace the
DateTimeZone
value with one of the available PHP timezones.Random:
UTC_TIMESTAMP(6)
for time with 6 digits of microseconds.MySQL 5.7+
MySQL:
UTC_TIMESTAMP()
PHP:
gmdate()
Also PHP
date_default_timezone_set()
is used in PHP to set the current time zone for the script. You can set it to the client time zone so all the formatting functions return the time in his local time.In truth though I had a hard time getting this to work and always stumble into some gotcha. Eg. time information returned from MySQL is not formatted as 'UTC' so strtotime transforms it into a local time if you are not careful. I'm curious to hear if someone has a reliable solution for this problem, one that doesn't break when dates traverse media boundaries (HTTP->PHP->MySQL and MySQL->PHP->HTTP), also considering XML and RSS/Atom.
NOW()
gives you the time (including the timezone offset) of the system running your database. To get UTC date/time you should useUTC_TIMESTAMP()
as described in the MySQL Reference Manual.https://dba.stackexchange.com/questions/20217/mysql-set-utc-time-as-default-timestamp
Quoting all the answer from above link in case of delete:
To go along with @ypercube's comment that
CURRENT_TIMESTAMP
is stored as UTC but retrieved as the current timezone, you can affect your server's timezone setting with the --default_time_zone option for retrieval. This allows your retrieval to always be in UTC.By default, the option is 'SYSTEM' which is how your system time zone is set (which may or may not be UTC!):
You can set this dynamically:
Or permanently in your my.cnf:
Restart your server, and you will see the change:
As @Haluk suggests, you can store the date as a UTC
datetime
. I'm complementing his answer for situations when the date you want to insert comes from PHP code, and adding some details about how it works :Actually
datetime
in PHP doesn't have a timezone associated to it. What is passed to PDO is just a string, in one of the formats recognized by MySQL, representing the date in UTC timezone. For example if the date is2015-10-21 19:32:33 UTC+2:00
, the code above just tells MySQL to insert2015-10-21 17:32:33
.gmtdate("Y-m-d H:i:s")
gives you the current date in such a format. In any case, all you need to do is build the string representing the date you want to insert in UTC time.Then, when you read the date, you have to tell PHP that the timezone of the date you just retrieved is UTC. Use the code in Haluk's answer for this.