简体   繁体   中英

Convert MySQL datetime to different timezones

I've been reading around here and Google about the way MySQL stores datetime and date, and I can't seem to get a good understanding of it, and what I need to do to accomplish my goals. I'm also not entirely sure my host is set up correctly as it doesn't seem to behave as I expect it to.

My goal is: Datetime and Date would be stored as UTC +00:00. Then, when the database is queried with PHP, the datetime and date would be converted based on the user's settings before being displayed.

Querying the server with SELECT @@global.time_zone, @@session.time_zone; returns SYSTEM for both.

Also, if I try doing a query with SET time_zone '-05:00'; it does not change the datetime that is output from the server. Changing it to any other value also does not change it.

MySQL only ever interacts with the server it's running on so it will only ever give the time zone of that server. You'd have to get the time zone on the client side .

Edit:

MySQL doesn't have a time zone literal so you can use UTC_TIMESTAMP() to get the current time in UTC or you can convert the time elsewhere.

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