【问题标题】:Timezone issues with MySQL vs PHPMySQL 与 PHP 的时区问题
【发布时间】:2016-11-30 18:40:55
【问题描述】:

背景:我有一些 PHP 代码可以根据各种规则计算下一次重复发送消息的时间。我还有一个健全性页面,可以仔细检查下一次重复是否正确。由于我不知道如何解决时区问题,理智页面显示了一些我不确定它们是否真的是问题的问题。

我相信这些会出现,因为(未来)日期是在 BST(英国夏令时)期间,但欧洲/伦敦时区目前是 GMT。

我创建了一个非常简化的测试页面来调试和测试一个示例。代码如下:

// Not included: DB connection setup, which includes a query to set the MySQL session timezone to the current timezone for Europe/London as calculated by PHP

// Copied from a comment at http://php.net/manual/en/function.date-default-timezone-get.php
function timezone_offset_string( $offset )
{
    return sprintf( "%s%02d:%02d", ( $offset >= 0 ) ? '+' : '-', abs( $offset / 3600 ), abs( $offset % 3600 ) );
}

$offset = timezone_offset_get( new DateTimeZone( date_default_timezone_get() ), new DateTime() );

// query_multiple() is a function to get a single result from the database and return it as the specified PDO type
$db = query_multiple("SELECT NextRecurrence, UNIX_TIMESTAMP(NextRecurrence) AS NextTS, IF(@@session.time_zone = 'SYSTEM', @@system_time_zone, @@session.time_zone) AS DBTZ FROM RecurringMessages WHERE RecurID=96 LIMIT 1", "", "", PDO::FETCH_ASSOC);

print "DB Date of NextRecurrence  : ".$db['NextRecurrence']."<br>";
print "DB timestamp of NextRecurrence  : ".$db['NextTS']."<br>";
print "DB timezone  : ".$db['DBTZ']."<br>";

print "PHP timezone and offset: " .date_default_timezone_get().", ".timezone_offset_string( $offset ) . "<br>";
print "PHP date of NextTS : ".date('Y-m-d H:i:s', $db['NextTS'])."<br>";

这个页面的输出如下:

DB Date of NextRecurrence : 2017-05-24 10:00:00
DB timestamp of NextRecurrence : 1495620000
DB timezone : +00:00
PHP timezone and offset: Europe/London, +00:00
PHP date of NextTS : 2017-05-24 11:00:00

如您所见,PHP 日期相差一小时,尽管 PHP 和 MySQL 时区相同。如上所述,我认为这是因为给出的日期是在英国夏令时期间,而欧洲/伦敦目前是格林威治标准时间。

如果我将 1495620000 输入 http://www.epochconverter.com/,它会告诉我现在是 2017 年 5 月 24 日星期三 10:00:00 GMT

目前有两件事我不确定

  1. 当前数据库条目是否正确?到 2017 年 5 月 24 日,这个消息是在 10:00 还是 11:00 发送的?用于发送消息的 PHP 代码在连接后立即为 MySQL 设置会话时区,以匹配欧洲/伦敦的当前时区。
  2. 如果数据库条目正确,我如何让 PHP 日期函数为我提供任何未来日期的正确输出?如果 PHP 日期函数是正确的,我如何(通常)用正确的日期更新 MySQL 记录?我只知道它应该在 2017 年 5 月 24 日 10:00 发送。目前查询更新为UPDATE SET NextRecurrence='2017-05-24 10:00:00' ...

【问题讨论】:

    标签: php mysql timezone


    【解决方案1】:

    我想通了(我想)。

    因为 MySQL 当前处于 GMT,我需要在 GMT 中向 MySQL 提供 Y-m-d H:i:s 格式的日期和时间,即使日期是在 BST 期间。

    // $ts contains the correct unix timestamp
    
    $offsetnow=date('Z');
    $tsoffset=date('Z', $ts);
    $diff=$offsetnow-$tsoffset;
    
    // Add the difference to the timestamp so that the PHP date returns the appropriately presented Y-m-d H:i:s to MySQL, which as of writing this is GMT.
    $ts+=$diff;
    
    $query="UPDATE Recurrences SET NextRecurrence='".date('Y-m-d H:i:s', $ts)."' ...
    

    如果有人发现我在做的事情有错误,请告诉我!

    【讨论】:

      猜你喜欢
      • 2012-01-21
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 2012-10-11
      • 2019-12-27
      • 2013-08-15
      相关资源
      最近更新 更多