【问题标题】:Why is strtotime($lastmoment) bigger than time()?为什么 strtotime($lastmoment) 比 time() 大?
【发布时间】:2009-12-15 11:14:27
【问题描述】:

以下代码

 echo $lastmoment."<br/>";
echo time();
echo "<br/>";
echo strtotime($lastmoment);

输出:

2009-12-15 17:40:53 1260876829 1260898853

怎么了? $lastment 是 MySQL 过去的时间戳,为什么 strtotime($lastmoment) 比 time() 大?

【问题讨论】:

  • 您能否将以下内容添加到您的代码中,在echo time() 上方:echo $lastmoment;?在使用 strtotime() 函数转换之前查看值会很有帮助。
  • 另外,可能在之前或之后连续多次回显$lastmoment,以证明它保持不变。谢谢。
  • $lastmoment 保持不变。
  • 见安德鲁的回答。您的服务器有两个不同的时钟。

标签: php mysql timestamp strtotime


【解决方案1】:

如果您的 MySQL 服务器是不同的机器,或者 $lastmoment 中的时间戳是从不同的机器设置的,您可能会看到时钟漂移。检查各种机器上的系统时钟,看看它们是否一致。

【讨论】:

    【解决方案2】:

    因为 time() 函数可能在 $lastmoment 之前几毫秒执行。

    编辑: 将评论添加到我的想法中,我可能认为 strtotime() 的转换可能失败了。 或者数据库中的日期值包含一个值,例如date() 没有的天数。 请务必比较相同格式的 2 个日期值。

    【讨论】:

    • $lastmoment 是从数据库中获取的,这是一个过去的时间戳。
    • $lastmoment 的值是固定的。
    • 如果 strtotime 失败,它应该抛出 -1,因此是 1969 年 12 月 31 日。即使数据库中的日期值更精确等等,strtotime 的全部意义在于转换任何日期/时代的到来。所以即使是毫秒,它也会四舍五入到纪元。
    猜你喜欢
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2017-03-05
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多