2014-12-19 120 views
0

使用日期和时间函数计算时间。错误计算时间 - strtotime,date() - PHP

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

我不知道为什么,但echo $totaltime06代替66

它的其他时间框架工作的罚款。即为12:30, 13:30

感谢您的任何帮助。

回答

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); 
+0

我只需要几分钟,像66应该是在这种情况下。 – Fahad 2014-12-19 11:22:49

+0

完成了。谢谢 – Fahad 2014-12-19 11:24:53

+0

然后乘以60小时,然后添加分钟 – 2014-12-19 11:24:58

2

由于日期( “I”)仅示出了从0分钟至59

PHP date manual

一种可能的解决方案:

$totaltime = (strtotime($endtime) - strtotime($starttime))/60; 
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

+0

当$ endtime =“01:01”它返回-654 – Fahad 2014-12-19 11:32:57

+0

echo $ totaltime = floor((strtotime($ endtime) - strtotime($ starttime))/ 60); – 2014-12-19 11:36:16

+0

是的,使用相同的...返回负值 – Fahad 2014-12-19 11:36:56