错误的计算时间 - strtotime,date() - PHP

时间:2014-12-19 11:15:58

标签: php datetime strtotime

使用日期和strtotime函数计算时间。

$starttime = '11:55';
$endtime = '13:01'; //or '1:01'
$totaltime = date("i",strtotime($endtime) - strtotime($starttime));

我不知道为什么,但echo $totaltime给予06代替66

它在其他时间框架上工作正常。即12:30, 13:30

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

因为日期(" i")仅显示从0到59的分钟。

PHP date manual

可能的解决方案:

$totaltime = (strtotime($endtime) - strtotime($starttime)) / 60;

答案 1 :(得分:2)

您的返回值被格式化为新的时间输出(因为date()函数)

由于您只是在格式化时间内请求分钟数,因此只返回该部分。

如果您想查看小时和分钟,请使用h:

输出
$totaltime = date("H:i",strtotime($endtime) - strtotime($starttime));

这将返回“01:06”。

如果您需要两个日期之间的实际分钟数(因此您希望66作为结果),那么您不是在寻找格式化的时间字符串作为结果,而是一个保持分钟数的常规整数。您可以根据之前的计算进行计算,如下所示:

// divide total seconds between these points by 60, round down.
$totalMinutes = floor ( ( strtotime($endtime) - strtotime($starttime) ) / 60 );

答案 2 :(得分:0)

<?php

$starttime = '11:55';
$endtime = '13:01';
echo $totaltime (strtotime($endtime) - strtotime($starttime)) / 60;
echo $totaltime = floor ((strtotime($endtime) - strtotime($starttime)) / 60);

?>

enter image description here