在没有超级权限的情况下更改MySQL时区

问题描述

| 我将此行添加到了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中设置日期时区?     

解决方法

        假设您使用的是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,or DATETIME values,convert them to UTC,perform the arithmetic,and then convert back. 
因此,在没有ѭ5的情况下尝试使用它,看看是否可行,并检查check6ѭ是否为您提供正确的时区。 编辑:您可能只是数据库有问题。我只是在我的一个数据库(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)
    ,        Erm我不确定如何使用mysql设置时区... 但我更喜欢用php设置时区...
date_default_timezone_set(\'Europe/Rome\');