0

尝试减去两次:

$obtime = "2310";
$runtime = "0048";

$run=new DateTime($runtime);
$ob=new DateTime($obtime);
$interval= $run->diff($ob);
$age=$interval->format('%H%I');
print "Age is: $age\n";

上面会输出 2222,意思是 22 小时 22 分钟的差异。当然,我们知道时钟是前进的,而 0048 距离 2310 仅 1 小时 38 分。

有没有更好的方法来查找两个“24 小时”时间之间的时差?

4

2 回答 2

1

怎么样

$obtime = strtotime("2310");
$runtime = strtotime("0048");
echo gmdate("H:i:s", $obtime - $runtime);
于 2013-10-10T02:58:10.010 回答
0

谢谢格威利。这并没有像你所拥有的那样工作,但是,这是因为时间变量的顺序。

如果您从较新的时间中减去较旧的时间,它会起作用。

这输出 1 小时 38 分钟:

$obtime = strtotime("2210");
$runtime = strtotime("2348");
echo gmdate("H:i", $runtime - $obtime);

虽然这输出 22 小时 22 分钟:

$obtime = strtotime("2210");
$runtime = strtotime("2348");
echo gmdate("H:i", $obtime - $runtime);

很高兴知道这种方法可以前进或后退,而我使用的原始方法并不那么“聪明”。

于 2013-10-10T03:13:17.147 回答