2011-05-29 69 views
2

我将此行添加到我的config.inc.php文件中。在没有超级权限的情况下更改MySQL时区

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

它不给我任何错误,但是当我使用NOW()CURRENT_TIMESTAMP()功能可以节省一个错误的时间记录。

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

+0

如果您指定时间偏移量为'+01:00',该怎么办? 。 – zerkms 2011-05-29 12:25:54

+0

@zerkms谢谢回答,但它不工作:( – 2011-05-29 13:51:53

回答

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上失败。所以你可能需要数据库升级。

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) 
+0

感谢您的回答它正确设置MySQL的时区,但它保留了CURRENT_TIMESTAMP不起作用()函数 – 2011-05-29 14:24:58

+0

@Aegidius:复制粘贴我们与你的疑问,请MySQL控制台样品应当一切正常 – zerkms 2011-05-29 14:36:14

+0

感谢您的回答CURRENT_TIMESTAMP() - > 2011-05-29 7时53分04秒 - SET TIME_ZONE =“欧洲/罗马” - > 0行 - CURRENT_TIMESTAMP( ) - > 2011-05-29 07:55:04 - version() - > 5.0.91-log ... :( – 2011-05-29 14:53:43

0

呃,我不知道如何设置MySQL的时区...

但我更愿意设置与PHP的时区...

date_default_timezone_set('Europe/Rome'); 
相关问题