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

标签 php mysql apache timezone

我将这一行添加到我的 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/

相关文章:

php - Laravel 7 __construct 中的用户数据

php - 学说模式创建找不到常量

mysql - 删除数据库中所有前缀为 test_ 的列的命令

php - 阻止下一个索引因完整性违规而增加

linux - 使目录只能由 root 读取。

php - 找出error_log的路径

php - 无法在 PHPStorm 中运行 PHPUnit 测试

php - 如何同时运行两个数组

mysql - 在 mysql 表中添加/保存/插入代码后无法正确显示 C/C++ 代码

php - Docker - 在不同网站的主机和容器上运行 Apache