/  Technology   /  How To Change MySQL Timezone in Query?

How To Change MySQL Timezone in Query?

 

It may be necessary from time to time to convert the MySQL time value from one timezone to another. You can use the convert_tz() function in a MySQL query to change the timezone and you can find out how to do that here.

 

The CONVERT_TZ function is used to change the time zone in MySQL queries using the CONVERT_TZ function. The syntax of the CONVERT_TZ function can be found below.

Syntax:

#start
convert_tz(value, from_timezone, to_timezone)
#end

Using the above function, you need to provide three input parameters: the time value to be converted, the time zone from which you want the value to be converted, and the time zone from which you want the value to be converted.

The time zone can be specified in two ways: as an offset or as a name.

Using the timezone offsets ‘+00:00’ and ‘-05:00’, below is an example of how to convert a literal time value from UTC to EST timezone, using the offsets ‘+00:00’ and ‘-05:00’ respectively for UTC and EST.

#start
select convert_tz('2023-02-7 10:00:00','+00:00','-05:00');
#end

In addition to specifying offsets, you can also specify time zones. As a result, in this case, you will have to download and install the MySQL time zones on your server in order to make it work. You can use this example to convert EST to Paris timezone by specifying the name of the time zone instead of the offset value.

#start
select convert_tz('2023-02-7 10:00:00','US/Eastern','Europe/Paris');
#end

It is also possible to convert MySQL date, time, and date time functions using the CONVERT_TZ function. It is shown here an example of how to change the time zone of the current time obtained using the NOW() function from UTC to EST

#start
select convert_tz(now(),'+00:00','-05:00');
#end

 

Leave a comment