gpt4 book ai didi

php - 无需 super 权限更改MySQL时区

转载 作者:行者123 更新时间:2023-11-29 03:45:32 24 4
gpt4 key购买 nike

我将这一行添加到我的 config.inc.php 文件中。

$query = "SET SESSION time_zone = 'Europe/Rome'";
if (mysql_query($query, DB_LINK) == FALSE) {
die(mysql_error(DB_LINK));
}

它没有给我任何错误,但是当我使用 NOW()CURRENT_TIMESTAMP() 函数时,它以错误的时间保存了记录。

如何在没有 super 权限的情况下在MySQL中设置日期时区?

最佳答案

假设您使用的是 5.5,如果您看到 http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html ,它说:

mysql> SET time_zone = timezone;

它还说:

The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.

The current time zone setting does not affect values displayed by functions such as UTC_TIMESTAMP() or values in DATE, TIME, or DATETIME columns. Nor are values in those data types stored in UTC; the time zone applies for them only when converting from TIMESTAMP values. If you want locale-specific arithmetic for DATE, TIME, or DATETIME values, convert them to UTC, perform the arithmetic, and then convert back.

所以在没有 SESSION 的情况下尝试它,看看它是否有效,并检查 select @@session.time_zone;
为您提供正确的时区。

编辑:您的数据库可能有问题。我刚刚在我的一个数据库 (5.5.8) 上试过这个并且它有效,但它在 5.0.51 上失败了。所以你可能需要升级数据库。

mysql> select CURRENT_TIMESTAMP();
+---------------------+
| CURRENT_TIMESTAMP() |
+---------------------+
| 2011-05-29 14:33:06 |
+---------------------+
1 row in set (0.00 sec)

mysql> set time_zone = 'Europe/Rome';
Query OK, 0 rows affected (0.00 sec)

mysql> select CURRENT_TIMESTAMP();
+---------------------+
| CURRENT_TIMESTAMP() |
+---------------------+
| 2011-05-29 16:33:11 |
+---------------------+
1 row in set (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.8-log |
+-----------+
1 row in set (0.00 sec)

关于php - 无需 super 权限更改MySQL时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6167681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com