MySQL | CONVERT_TZ method
Start your free 7-days trial now!
MySQL's CONVERT_TZ(~)
method converts a datetime value from one timezone to another.
Parameters
1. datetime
| datetime
The datetime to convert from one timezone to another.
2. from_tz
| timezone
The original timezone to convert from.
3. to_tz
| timezone
The timezone to convert to.
Return value
The datetime value converted to the timezone passed as to_tz
argument.
Examples
Basic usage
To convert current local time in Tokyo to EST (Eastern Standard Time):
SELECT NOW(), CONVERT_TZ(NOW(), 'Asia/Tokyo','America/New_York');
+---------------------+----------------------------------------------------+| NOW() | CONVERT_TZ(NOW(), 'Asia/Tokyo','America/New_York') |+---------------------+----------------------------------------------------+| 2020-05-19 22:32:17 | 2020-05-19 09:32:17 |+---------------------+----------------------------------------------------+
We convert the current time in Tokyo to equivalent time in New York.
To convert time using offset format:
SELECT CONVERT_TZ('2020-04-26 17:32:11', '+08:00', '-05:00');
+-------------------------------------------------------+| CONVERT_TZ('2020-04-26 17:32:11', '+09:00', '-05:00') |+-------------------------------------------------------+| 2020-04-26 04:32:11 |+-------------------------------------------------------+
Here +08:00
represents Tokyo time while -05:00
represents New York time. These are both offsets from UTC (Coordinated Universal Time).
Troubleshooting
Linux and macOS
If the above queries in the example return NULL
, make sure you have loaded the time zone table into mysql. The MySQL installation procedure creates the time zone tables, but does not load them. Hence we have to load them manually by running the below in a terminal command line:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
Here, note the following:
mysql_tzinfo_to_sql
is the program that loads information to the time zone tables./usr/share/zoneinfo
is where we fetch the set of files describing time zones.-u
is the flag for username. The account must have privileges for modifying tables in the mysql system schema.-p
is the flag for password.
If you face an error such as:
Warning: Unable to load '/usr/share/zoneinfo/+VERSION' as time zone.
You can override this by adding the force switch:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql --force
Windows
If the above queries in the example return NULL
, you will need to proceed with the following steps to load information into the time zone tables in mysql:
1. Download the time zone package files from https://dev.mysql.com/downloads/timezones.html
2. Unpack the files and move them to your MySQL directory (something like C:\ProgramData\MySQL\MySQL Server 8.0\data\mysql
)
3. Load the time zone information in the files using the below command:
mysql -u root -p mysql < file_name
Here, note the following:
-u
is the flag for username. The account must have privileges for modifying tables in the mysql system schema.-p
is the flag for password.file_name
is the name of the unpacked file from step 2.
4. Restart your MySQL server.