【问题标题】:PHP - Why does time() return different number of seconds than strtotime(date())PHP - 为什么 time() 返回的秒数与 strtotime(date()) 不同
【发布时间】:2016-02-16 20:46:14
【问题描述】:

我发现这两个返回不同的数字 - 但都应该给出时间戳。

strtotime( date("Y-m-d h:i:s") ); //currently 1447570497
time(); //currently 1447613697

差值正好是 12 小时(43200 秒),同样的情况也发生在:

(new DateTime( date("Y-m-d h:i:s") ))->getTimestamp(); //acts like strtotime+date
(new DateTime("now"))->getTimestamp(); //acts like time()

为什么会这样? (因为是 12 小时,所以可能不是夏令时问题)

是 24 小时还是 12 小时模式?

【问题讨论】:

  • 你为什么要这样?
  • h 是 1-12。因此,如果您在早上进行测试,它会起作用:)
  • @Reeno 啊谢谢!这正是我的问题 - 它有效,然后它没有 x) !

标签: php date datetime time strtotime


【解决方案1】:

您需要在date() 中使用H 来获取中午12 点之后的正确时间。否则您将在 1 到 12 小时前离开。

strtotime( date("Y-m-d H:i:s") ); //currently 1447570497

【讨论】:

    猜你喜欢
    • 2014-04-11
    • 2013-06-09
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多