如何在 MySQL 選擇查詢中將 UTC 日期轉換為本機時區?

Barbara Streisand
發布: 2024-11-15 02:41:02
原創
777 人瀏覽過

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');
登入後複製

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
登入後複製

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;
登入後複製
  • 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.

以上是如何在 MySQL 選擇查詢中將 UTC 日期轉換為本機時區?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板