I want to make it so calls to NOW() and CURDATE() in MySQL queries return the date in UTC. How do I make this happen without going through and changing all queries that use
The proper way to do this is to change your server's time zone to UTC, just as MarkR said.
However, it's also possible to use SET time_zone to change the time zone for your current session.
From the manual:
The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME()
UTC_TIMESTAMP()
Returns the current UTC date and time as a value in 'YYYY-MM-DD hh:mm:ss' or YYYYMMDDhhmmss.uuuuuu format, depending on whether the function is used in a string or numeric context.
UTC_DATE()
Returns the current UTC date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
UTC_TIME()
Returns the current UTC time as a value in 'hh:mm:ss' or hhmmss.uuuuuu format, depending on whether the function is used in a string or numeric context.
MySQL reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_utc-timestamp
If changing the timezone on your running production servers or updating a key configuration setting and restarting mysql seems unrealistic and/or overkill, try this:
CONVERT_TZ(NOW(), 'US/Pacific', 'UTC')
Where US/Pacific
is the timezone your NOW()
call is returning the time in.
Finally found what I was looking for...
In my.cnf,
[mysqld_safe]
timezone = UTC
I was putting this option under [mysqld], and mysql was failing to start.
Calling "SET time_zone='+0:00';" on every page load would also work, but I don't like the idea of calling that query on every single page load.
Goto /etc/mysql/my.cnf file and add this below line under [mysqld] section
default-time-zone = '+00:00'
Then restart your mysql. Now select curtime(); shows the GMT time.
You will need to use the SET TIMESTAMP statement to format the datetime results in the desired format. This will mean changing all those queries. sysdate() will not obey this though.