MySQL convert_tz
The MySQL convert_tz() function is a handy function to have. It allows you to convert a datetime value to another timezone. This is done by specifying the offset for the source and destination timezones. This can be very useful, for example, if you have logged values into your database in one region and then want to store them in another database for a different region that records the date in its own time zone.
Syntax:
CONVERT_TZ (dt, from_tz,to_tz)
Parameters:
Name | Description |
dt | A datetime. |
from_tz | A time zone which will be converted to to_tz. |
to_tz | A time zone in which the from_tz will convert. |
Example:
Input | 2018-07-12 12:00:00 |
Output | 2018-07-12 22:00:00 |
SELECT CONVERT_TZ('2018-07-12 12:00:00','+00:00','+10:00');
This will convert this input: 2018-07-12 12:00:00 To this output: 2018-07-12 22:00:00
Here are a few more examples:
SELECT CONVERT_TZ('2020-09-15 23:59:59', '+00:00', '+05:50');
SELECT CONVERT_TZ('2020-09-15 23:59:59', '+00:00', '-05:50');
SELECT CONVERT_TZ('2020-09-15 23:59:59', '+00:00', '+10:30');