Home > Database > Mysql Tutorial > body text

How to Convert UTC Dates to Local Time Zones in MySQL Select Queries?

Barbara Streisand
Release: 2024-11-15 02:41:02
Original
831 people have browsed it

How to Convert UTC Dates to Local Time Zones in MySQL Select Queries?

Converting UTC Dates to Local Time Zones in MySQL Select Queries

Question:

How can I modify a MySQL query to convert a UTC timestamp column, "displaytime," into the local time zone?

Answer 1:

Use the CONVERT_TZ() function as follows:

SELECT CONVERT_TZ(displaytime, 'GMT', 'MET');
Copy after login

Answer 2:

Ensure that your timezone tables are initialized. Use the mysql_tzinfo_to_sql program to populate them:

shell> mysql_tzinfo_to_sql /usr/share/zoneinfo
Copy after login

Once initialized, you can use the CONVERT_TZ() function as described above.

Additional Notes:

  • Check your timezone-related tables using the following queries:
SELECT * FROM mysql.time_zone;
SELECT * FROM mysql.time_zone_name;
Copy after login
  • If they are empty, you need to initialize them as described above.
  • Ensure that your column type is either timestamp or date for the CONVERT_TZ() function to work correctly.
  • Specify the time zones in the CONVERT_TZ() function as either HH:MM (for positive offsets from UTC) or -HH:MM (for negative offsets). For example, 00:00 for UTC and 04:00 for Moscow Standard Time.

The above is the detailed content of How to Convert UTC Dates to Local Time Zones in MySQL Select Queries?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template