如果小时大于24,则两个时间戳之间的差值


Diffrence between two time stamp if hour greater than 24

我刚刚编辑了我的问题我有两种时间格式,我想知道它们之间的区别

例如

 $time1 = new DateTime('09:00:59');
 $time2 = new DateTime('100:30:00');
 $interval = $time1->diff($time2);
   echo $interval->format('%h:%i:%s second(s)'); 
  ?>

如果我增加time2,它在24小时内正常工作,显示致命错误

$time2 = new DateTime('100:30:00');

致命错误:未捕获的异常' exception '伴有消息'DateTime::__construct() [DateTime . dat]。——construct]:在位置0(1)处解析时间字符串(100:30:00)失败:在D:'xampp'htdocs' DateTime .php:3堆栈跟踪:#0 D:'xampp'htdocs' DateTime .php(3): DateTime->__construct('100:30:00') #1 {main}抛出在D:'xampp'htdocs' DateTime .php第3行

是否有其他方法或我可以编辑相同的我已经尝试了很多,但没有找到解决方案我只想用任意方法求差值由于

一种方法:

$time2 = '100:00:00';
$time1 = '10:30:00';
list($hours, $minutes, $seconds) = explode(':', $time2);
$interval2 = $hours*3600 + $minutes*60 + $seconds;
list($hours, $minutes, $seconds) = explode(':', $time1);
$interval1 = $hours*3600 + $minutes*60 + $seconds;
$diff = $interval2 - $interval1;
echo floor($diff / 3600) . ':' . 
     str_pad(floor($diff / 60) % 60, 2, '0') . ':' . 
     str_pad($diff % 60, 2, '0');
输出:

<>之前89:30:00之前

这里是Codepad demo

希望这对你有帮助。

$time1 = '10:30:00';
$time2 = '100:00:00';

function hms2sec ($hms) {
    list($h, $m, $s) = explode (":", $hms);
    $seconds = 0;
    $seconds += (intval($h) * 3600);
    $seconds += (intval($m) * 60);
    $seconds += (intval($s));
    return $seconds;
}
$ts1=hms2sec($time2);
$ts2=hms2sec($time1);
$time_diff = $ts1-$ts2;
function seconds($seconds) {
        // CONVERT TO HH:MM:SS
        $hours = floor($seconds/3600);
        $remainder_1 = ($seconds % 3600);
        $minutes = floor($remainder_1 / 60);
        $seconds = ($remainder_1 % 60);
        // PREP THE VALUES
        if(strlen($hours) == 1) {
            $hours = "0".$hours;
        }
        if(strlen($minutes) == 1) {
            $minutes = "0".$minutes;
        }
        if(strlen($seconds) == 1) {
            $seconds = "0".$seconds;
        }
        return $hours.":".$minutes.":".$seconds;
        }
echo $final_diff=seconds($time_diff);

因为我没有足够的"声誉"来为所选的答案添加评论。我想添加一条信息来指出它的一个缺陷。

如果您尝试使用这些参数:

$time2 = '10:15:00';
$time1 = '10:10:00';

你会得到一个错误的结果:0:50:00

要纠正这个问题,需要在处理分钟的str_pad中添加STR_PAD_LEFT,如:

str_pad(floor($diff / 60) % 60, 2, '0', STR_PAD_LEFT) . ':' .