【问题标题】:Change MySQL time zone without super permissions在没有超级权限的情况下更改 MySQL 时区
【发布时间】:2011-09-04 06:59:41
【问题描述】:

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

【问题讨论】:

  • 如果用时间+01:00指定时间偏移怎么办?
  • @zerkms 感谢您的回答,但它不起作用:(

标签: php mysql apache timezone


【解决方案1】:

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

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

date_default_timezone_set('Europe/Rome');

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您的回答。它正确设置了 MySQL 时区,但它对 CURRENT_TIMESTAMP() 函数不起作用。
    • @Aegidius:请复制粘贴我们的 mysql 控制台示例和您的查询。一切都应该正常
    • 感谢您的回答。 CURRENT_TIMESTAMP() -> 2011-05-29 07:53:04 - SET time_zone = 'Europe/Rome' -> 0 行 - CURRENT_TIMESTAMP() -> 2011-05-29 07:55:04 - version() -> 5.0.91-log ... :(
    • 是的,在 5.0 系列上不能正常工作。我觉得你运气不好。您可能想看看使用 dev.mysql.com/doc/refman/5.0/en/… 函数。
    • 真的很感谢你帮助我。我刚刚删除了所有 MySQL 函数并用 PHP 函数替换了它们。再次感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    相关资源
    最近更新 更多