无需超级权限即可更改MySQL时区

发布于 2024-11-10 11:31:09 字数 322 浏览 1 评论 0原文

我将此行添加到我的 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() 函数时,它会保存错误时间的记录。

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

I added this line to my config.inc.php file.

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

It doesn't give me any error, but when I use the NOW() or the CURRENT_TIMESTAMP() function it saves the record with a wrong time.

How can I set the date time zone in MySQL without the super permissions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的影子我的梦 2024-11-17 11:31:09

假设您使用的是 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)

Assuming you are using 5.5, if you see http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html, it says:

mysql> SET time_zone = timezone;

It also says:

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. 

So try it without the SESSION and see if it works, and also check that select @@session.time_zone;
gives you the right timezone.

EDIT: you may just have an issue with your database. I just tried this on one of my databases (5.5.8) and it worked, but it failed on 5.0.51. So you may need a db upgrade.

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)
樱娆 2024-11-17 11:31:09

呃,我不知道如何用 mysql 设置时区...

但我更喜欢用 php 设置时区...

date_default_timezone_set('Europe/Rome');

Erm I'm not sure how to set time zone with mysql...

but i prefer to set the time zone with php...

date_default_timezone_set('Europe/Rome');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文