Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果总时间大于24小时,用php计算数组的总时间_Php_Arrays_Sum - Fatal编程技术网

如果总时间大于24小时,用php计算数组的总时间

如果总时间大于24小时,用php计算数组的总时间,php,arrays,sum,Php,Arrays,Sum,我想得到数组中时间的总和。在提出这个问题之前,有很多问题要问。唯一的问题这个解决方案有效唯一的总和是不到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 ($

我想得到数组中时间的总和。在提出这个问题之前,有很多问题要问。唯一的问题这个解决方案有效唯一的总和是不到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);
?>
预期结果

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);
?>

看看你在做什么:利用时间进行计算,忽略日期部分。 也许用另一种方式来考虑问题:1小时=60秒*60分钟。因此,将所有迭代转换为秒,在结束时求和,然后自己编写所需的时间

或者,或者你会使用一些更伟大的东西


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

$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);

就我个人而言,我会完全避免接触任何日期函数,因为你没有处理日期。你可以这样做:

// 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
因此,我们通过:循环输入数组中的项和字符串,以获得一个包含索引0、1和2中的小时、分钟和秒的数组


然后,我们将这些值转换为秒,并将其添加到总数中。完成后,我们将格式重新设置为
HH:MM:SS
format

这在某种程度上是正确的。我还想得到第二个时间,不是每一次,兄弟。实际上,我想计算每个员工的工作时间总数。做一点小小的改变有那么难吗<代码>%d天%H小时
而不是
%m月,%d天
。并将日期时间转换为小时。mktime()将帮助您。您必须每天24*n个工作日+当前工作小时。
$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);
// 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