为什么 CONVERT_TZ() 在我的 MySQL 查询中不起作用?

Mary-Kate Olsen
发布: 2024-11-15 07:25:02
原创
850 人浏览过

Why Is CONVERT_TZ() Not Working in My MySQL Queries?

Converting UTC Dates to Local Time Zones in MySQL Queries

To convert UTC timestamps to local time zones in your MySQL queries, you can use the CONVERT_TZ() function. However, it's important to note that using this function may not always be successful.

Why CONVERT_TZ() Might Fail

If CONVERT_TZ() is not working for you, there are a few possible reasons:

  • Incorrect Time Zone Specifications: Ensure that you have provided valid time zone identifiers in the CONVERT_TZ() function.
  • Empty Time Zone Tables: MySQL relies on time zone tables to perform time zone conversions. If these tables are empty, you need to initialize them using the mysql_tzinfo_to_sql program.

Query Example Using CONVERT_TZ()

The following query converts a timestamp column called displaytime to the MET time zone:

SELECT CONVERT_TZ(displaytime, '+00:00', '+04:00');
登录后复制

Checking Time Zone Tables

To verify if your time zone tables are initialized, execute the following queries:

SELECT * FROM mysql.time_zone;
SELECT * FROM mysql.time_zone_name;
登录后复制

Initializing Time Zone Tables

If the time zone tables are empty, use the following command to load them:

mysql_tzinfo_to_sql /usr/share/zoneinfo
登录后复制

Once the time zone tables are initialized, try using CONVERT_TZ() again. If you still encounter issues, consult the MySQL documentation for more detailed troubleshooting steps:

  • [Date and Time Functions: CONVERT_TZ()](https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz)
  • [Time Zone Support](https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html)
  • [mysql_tzinfo_to_sql](https://dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html)

以上是为什么 CONVERT_TZ() 在我的 MySQL 查询中不起作用?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板