【问题标题】:PHP strtotime not performing similar conversion with mysql valuePHP strtotime不使用mysql值执行类似的转换
【发布时间】:2018-08-18 00:47:05
【问题描述】:

所以我从 mysql 数据库中提取时间戳,想知道为什么所有时间检查都不起作用。

echo "The time is " . date("Y-m-d h:i:s");
echo "</br>";
echo "Mysql Timestamp " . $row["Uptime"];
echo "</br>";
echo "Mysql Timestamp as strtotime" .strtotime($row["Uptime"]);
echo "</br>";
echo "strtotime Current Time". strtotime("now");
echo "</br>";

输出:

时间是2018-08-18 12:42:55 Mysql 时间戳 2018-08-18 12:42:52 Mysql时间戳为strtotime1534596172 strtotime 当前时间1534552975

转成字符串后的数字相差约4000。发生了什么事以及如何在不执行 x-4000 的情况下解决此问题?

【问题讨论】:

    标签: php mysql timestamp strtotime


    【解决方案1】:

    差异是由于您当地的时区。 date() 使用您当地的时区来计算时间,而 strtotime("now")time() 使用 UTC 时间。比较:

    echo "The time is " . date("Y-m-d h:i:s");
    echo "</br>";
    echo 'strtotime("now") is '. strtotime("now");
    echo "</br>";
    echo 'strtotime date("Y-m-d h:i:s") is '. strtotime(date("Y-m-d h:i:s"));
    echo "</br>";
    echo "time() is ". time();
    echo "</br>";
    

    输出(时区Australia\Adelaide):

    The time is 2018-08-17 09:06:35
    strtotime("now") is 1534554395
    strtotime date("Y-m-d h:i:s") is 1534511195
    time() is 1534554395
    

    现在尝试将时区设置为UTC

    date_default_timezone_set('UTC');
    echo "The time is " . date("Y-m-d h:i:s");
    echo "</br>";
    echo 'strtotime("now") is '. strtotime("now");
    echo "</br>";
    echo 'strtotime date("Y-m-d h:i:s") is '. strtotime(date("Y-m-d h:i:s"));
    echo "</br>";
    echo "time() is ". time();
    echo "</br>";
    

    输出 - 如您所见,所有表单都返回相同的值:

    The time is 2018-08-18 01:07:21
    strtotime("now") is 1534554441
    strtotime date("Y-m-d h:i:s") is 1534554441
    time() is 1534554441
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 2014-04-29
      • 2011-05-29
      • 2014-02-28
      • 2011-11-20
      相关资源
      最近更新 更多