如果总时间大于 24 小时,则以 php 为单位计算数组的总时间



我想获取数组中的时间总和。在与这个问题相关之前,有很多问题被问到。唯一的问题,此解决方案工作的唯一总和小于24小时。24 小时后,它将在 00:00:00 开始。我如何获得超过 24 小时的总时长?

<?php
$total = [
'00:02:55',
'00:07:56',
'01:03:32',
'15:13:34',
'02:13:44',
'03:08:53',
'13:13:54'
];
$sum = strtotime('00:00:00');
$sum2=0;
foreach ($total as $v){
$sum1=strtotime($v)-$sum;
$sum2 = $sum2+$sum1;
}
$sum3=$sum+$sum2;
echo date("H:i:s",$sum3);
?>

结果

11:04:28

预期成果

35:04:28

演示链接

尝试以下代码

<?php
function explode_time($time) { //explode time and convert into seconds
$time = explode(':', $time);
$time = $time[0] * 3600 + $time[1] * 60;
return $time;
}
function second_to_hhmm($time) { //convert seconds to hh:mm
$hour = floor($time / 3600);
$minute = strval(floor(($time % 3600) / 60));
if ($minute == 0) {
$minute = "00";
} else {
$minute = $minute;
}
$time = $hour . ":" . $minute;
return $time;
}
$time = 0;
$time_arr =  [
'00:02:55',
'00:07:56',
'01:03:32',
'15:13:34',
'02:13:44',
'03:08:53',
'13:13:54'
];
foreach ($time_arr as $time_val) {
$time +=explode_time($time_val); // this fucntion will convert all hh:mm to seconds
}
echo second_to_hhmm($time);
?>

使用外部日期时间扩展 dt,您可以将所有时间添加到日期。 使用 DateTime::d iff,您可以得到结果:

$dt = dt::create("2000-1-1");  //fix Date
$dtsum = clone $dt;
foreach($total as $time){
$dtsum->addTime($time);
}
$diff = $dt->diff($dtsum);
printf('%d:%02d:%02d',$diff->days * 24 + $diff->h,$diff->i,$diff->s);

输出: 35:04:28

没有日期时间扩展名的更新

$dt = date_create("2000-1-1");  //fix Date
$dtsum = clone $dt;
foreach($total as $time){
$timeArr = explode(":",$time);
$secondsAdd = $timeArr[0] * 3600 + $timeArr[1] * 60 +$timeArr[2];
$dtsum->modify($secondsAdd." Seconds");
}
$diff = $dt->diff($dtsum);
printf('%d:%02d:%02d',$diff->days * 24 + $diff->h,$diff->i,$diff->s);

看看你在做什么:利用时间进行计算,忽略日期部分。 也许换个角度考虑:1小时=60秒*60分钟。因此,将所有迭代转换为秒,最后做总和并编写您需要的时间。

或者,或者你会使用php文档中一些更伟大的东西

<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');

适应您的需求,我相信它会很好地工作。

就个人而言,我会完全避免接触任何日期函数,因为您没有使用日期。您可以执行以下操作:

// Input data
$data = [
'00:02:55',
'00:07:56',
'01:03:32',
'15:13:34',
'02:13:44',
'03:08:53',
'13:13:54'
];    
// Total to hold the amount of seconds
$total = 0;
// Loop the data items
foreach($data as $item):
$temp = explode(":", $item); // Explode by the seperator :
$total+= (int) $temp[0] * 3600; // Convert the hours to seconds and add to our total
$total+= (int) $temp[1] * 60;  // Convert the minutes to seconds and add to our total
$total+= (int) $temp[2]; // Add the seconds to our total
endforeach;
// Format the seconds back into HH:MM:SS
$formatted = sprintf('%02d:%02d:%02d', ($total / 3600),($total / 60 % 60), $total % 60);
echo $formatted; // Outputs 35:04:28

因此,我们循环输入数组中的项目并通过 :explode字符串,以获得索引 0、1 和 2 中包含小时、分钟和秒的数组。

然后,我们将每个值转换为秒并添加到我们的总数中。完成后,我们重新格式化为HH:MM:SS格式

最新更新