Use CONVERT_TZ() or SET time_zone to shift DATETIME/TIMESTAMP values between IANA or offset-based time zones.
CONVERT_TZ(datetime_expr, from_tz, to_tz) shifts a DATETIME or TIMESTAMP from one time zone to another and returns a new value. The original column stays unchanged, making it safe for reporting or display.
MySQL uses internal zone tables. If they are empty, CONVERT_TZ returns NULL.Load them once: mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
, then run FLUSH PRIVILEGES
.
Execute SELECT Name FROM mysql.time_zone_name ORDER BY Name;
or simply run SHOW VARIABLES LIKE 'system_time_zone';
to see the server’s default zone.
Run: SELECT id, CONVERT_TZ(order_date,'America/New_York','UTC') AS order_date_utc FROM Orders;
This produces UTC values you can feed into analytics without daylight-saving inconsistencies.
Store timestamps in UTC, pass the user’s IANA zone through a session variable, then convert on SELECT: SELECT id, CONVERT_TZ(order_date,'UTC',@user_tz) AS local_time FROM Orders;
.
Issue SET time_zone='America/Los_Angeles';
All subsequent TIMESTAMP values are assumed to be in that zone when stored and converted back on retrieval.
Local storage complicates daylight-saving transitions.Keeping all data in UTC avoids ambiguous or missing hours and simplifies cross-region queries.
Store UTC in tables, validate zone tables, convert only at the edge, and cache frequent CONVERT_TZ results in views or materialized reports for performance.
.
Convert the integer to DATETIME with FROM_UNIXTIME(), then pass it to CONVERT_TZ. Example: CONVERT_TZ(FROM_UNIXTIME(unixts),'UTC','Asia/Tokyo')
.
Run an UPDATE: UPDATE Orders SET order_date = CONVERT_TZ(order_date,'America/New_York','UTC');
Always back up first.
Either the zone name is misspelled or the mysql.time_zone tables are empty. Verify with SELECT COUNT(*) FROM mysql.time_zone_name;
.