📜  convert_tz mysql (1)

📅  最后修改于: 2023-12-03 15:00:01.952000             🧑  作者: Mango

Convert_tz in MySQL

Convert_tz is a built-in function in MySQL that allows you to convert a datetime value from one timezone to another. This is particularly useful when dealing with time values that are stored in different timezones or when you need to display date and time values in a specific timezone.

Syntax

The basic syntax of the convert_tz function is as follows:

CONVERT_TZ(dt, from_tz, to_tz)

Here, dt is the datetime value that you want to convert, from_tz is the current timezone, and to_tz is the timezone to which you want to convert the datetime value.

Examples
Example 1: Convert datetime value to a different timezone
SELECT CONVERT_TZ('2022-12-25 12:00:00', '+00:00', '+08:00');

This query will convert the datetime value 2022-12-25 12:00:00 from the UTC timezone (+00:00) to the GMT+8 timezone (+08:00). The output will be:

2022-12-25 20:00:00
Example 2: Convert datetime value from a timezone with DST to a different timezone with DST
SELECT CONVERT_TZ('2022-06-01 12:00:00', 'America/New_York', 'Europe/London');

In this example, the datetime value 2022-06-01 12:00:00 is converted from the New York timezone (which observes DST) to the London timezone (which also observes DST). The output will be adjusted based on the Daylight Saving Time (DST) rules in effect in each timezone.

Example 3: List all available timezones
SELECT * FROM mysql.time_zone_name;

This query will return a list of all available timezones in MySQL.

Conclusion

The convert_tz function is a powerful tool for working with datetime values in MySQL. By converting values between different timezones, you can ensure that your applications display and process time values accurately, regardless of their original timezone.