dongnai3960 2011-05-29 12:19
浏览 104
已采纳

没有超级权限更改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?

  • 写回答

2条回答 默认 最新

  • dpiuqwwyu187975836 2011-05-29 14:14
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)