简体   繁体   中英

Set timezone in mysql not working on datetime?

I've got a datetime colum named creation_date . when im using the SET time zone its still showing the same result. What am I doing wrong?

My Code:

SET time_zone = "America/Los_Angeles";
SELECT *  FROM `enswitch_mobile_users` WHERE `creation_date` > '2013-06-02 00:00:00';

Personally, I would stick with DATETIME and always use UTC_TIMESTMP() instead of NOW() or similar. Then when reading your date from the database, you can convert it with CONVERT_TZ() as needed.

datetime mysql data type doesn't care of time_zone directive. Only timestamp does

So the solution - change the type to timestamp

You can find this in the MySQL documentation on date and time types :

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM