【问题标题】:Php strtotime not sum vaulesphp strtotime不是总和值
【发布时间】:2016-03-07 10:31:59
【问题描述】:

您好,我正在使用此代码进行测试。

 <?php echo date("H:i:s", strtotime('00:00:10') + strtotime('00:00:10')); ?>

但输出是“15:32:04”而不是“00:00:20”,我找不到解决方案。

我也试试:

&lt;?php echo gmdate("H:i:s", strtotime('00:00:10') + strtotime('00:00:10')); ?&gt;

给我同样的输出。

感谢您的帮助

【问题讨论】:

  • echo date("H:i:s", strtotime('00:00:10 + 10 seconds'));
  • strtotime() 创建一个时间戳值(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),如果您不指定日期,则默认为当前日期,(今天)给出1457305210strtotime('00:00:10')(即2016-03-07 00:00:10);所以你不能简单地将这些时间戳加在一起,除非你强制日期为 Jan 01 1970
  • strtotime 返回一个时间戳。添加 2 个时间戳意味着您不仅要添加秒数,还要添加年数。

标签: php time strtotime


【解决方案1】:

您应该使用 dateTime 类: http://php.net/manual/en/class.datetime.php

一旦你开始使用它就会简单得多,而且你几乎可以通过调用一个函数来做任何你想做的事情。例如,您可以通过调用 add 函数将 X 时间添加到日期对象: http://php.net/manual/en/datetime.add.php

请记住,只有当您拥有 php 5.3 或更高版本时,该类才会存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-02
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多