我将此行添加到我的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中设置日期时区?
答案 0 :(得分:0)
嗯我不知道如何用mysql设置时区...
但我更喜欢用php设置时区......
date_default_timezone_set('Europe/Rome');
答案 1 :(得分:0)
假设您使用的是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上失败了。所以你可能需要db升级。
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)