查找两个时间戳之间的差异


Find the difference between two time stamps

$diff = strtotime(12:00:00) - strtotime(5:01:29);
echo date('H:i:s', $diff);

结果是 12:01:29

尝试获得 5 小时 1 分 29 秒的结果

我更喜欢使用 DateTime 类进行时间比较/加法/减法:

$dt1 = new DateTime();
$dt1->setTimestamp(strtotime('12:00:00am'));
$dt2 = new DateTime();
$dt2->setTimestamp(strtotime('5:01:29am'));
$interval = $dt1->diff($dt2);
echo $interval->format('%h hrs %i min %s sec');

编辑:使用您在 strtotime 中给出的格式,它考虑的是上午 5:01 到中午 12 点 - 所以它给出的答案与您想要的答案非常不同,因此您需要指定 am/pm