php添加分钟到目前为止

时间:2018-05-28 10:27:30

标签: php time strtotime

$orderDate = date('d/m/Y H:i:s', strtotime($result[$x]['date_modified']) );
echo "<br>".$orderDate;
$endTime =date('d/m/Y H:i:s',strtotime('+10 minutes', $orderDate));
echo "<br>".date('d/m/Y H:i:s', $endTime);

打印为

28/05/2018 09:27:45 - 这是正确的

  

注意:遇到的格式不正确的数值   第4166行的C:\ xampp5.5 \ htdocs \ php \ index.php

     

注意:遇到的格式不正确的数值   第4167行的C:\ xampp5.5 \ htdocs \ php \ index.php

01/01/1970 12:00:01 - 为什么这不起作用?

3 个答案:

答案 0 :(得分:2)

strtotime函数不适用于某些日期格式,d/m/Y是其中一种格式。为了正确处理日期和时间,推荐的格式是MySQL日期格式,即YYYY-mm-dd,并进一步处理它。

进一步阅读:The strtotime documentation reads

  

通过查看各个组件之间的分隔符来消除m / d / y或d-m-y格式的日期:如果分隔符是斜杠(/),则假设为美国m / d / y;而如果分隔符是破折号( - )或点(。),则假定为欧洲d-m-y格式。

答案 1 :(得分:0)

使用DateTime::createFromFormat接受所有类型的日期和时间格式

$dt = DateTime::createFromFormat('d/m/Y h:i:s', '28/05/2018 09:27:45');
$dt->add(new DateInterval('PT10M'));
echo $dt->format('d/m/Y h:i:s');

Live Demo

答案 2 :(得分:0)

$orderDateTimestamp = strtotime($result[$x]['date_modified']);
$endDateTimeStamp = strtotime('+10 minutes', $orderDateTimestamp);
$orderDate = date('d/m/Y H:i:s', $orderDateTimestamp);
$endDate = date('d/m/Y H:i:s', $endDateTimeStamp);
echo $orderDate.'<br>'.$endDate;

strtotime期望第二个参数是一个整数(时间戳)。此外,它接受的不是所有格式的字符串源,例如d/m/Y(也请检查$result[$x]['date_modified']的格式,如果它来自MySql结果集,则不应该有{{1} })。 所以使用这个解决方案来纠正它,它应该工作