用php计算经过的时间-跨越24小时

用php计算经过的时间-跨越24小时,php,datetime,time,add,Php,Datetime,Time,Add,我试图做的是将跨越24小时的小时/分/秒相加,例如: 12:39:25 08:22:10 11:08:50 07:33:05 我希望它返回的是39:43:30,而不是1970年的日期。下面是我目前正在使用的代码注释-它来自一个类,而不仅仅是一个函数 private function add_time($time1, $time2) { $first_exploded = explode(":", $time1); $second_exploded = explode(":", $time2);

我试图做的是将跨越24小时的小时/分/秒相加,例如:

12:39:25
08:22:10
11:08:50
07:33:05
我希望它返回的是39:43:30,而不是1970年的日期。下面是我目前正在使用的代码注释-它来自一个类,而不仅仅是一个函数

private function add_time($time1, $time2)
{
$first_exploded = explode(":", $time1);
$second_exploded = explode(":", $time2);
$first_stamp = mktime($first_exploded[0],$first_exploded[1],$first_exploded[2],1,1,1970);
$second_stamp = mktime($second_exploded[0],$second_exploded[1],$second_exploded[2],1,1,1970);
$time_added = $first_stamp + $second_stamp;
$sum_time = date("H:i:s",$time_added);
return $sum_time;
}

如果您有任何建议,我们将不胜感激。

日期功能始终围绕/使用日/月/年。 你想要的是一个简单的数学函数,没有测试它,但应该弄清楚

private function add_time($base, $toadd) {
     $base = explode(':', $base);
     $toadd = explode(':', $toadd);

     $res = array();
     $res[0] = $base[0] + $toadd[0];
     $res[1] = $base[1] + $toadd[1];
     $res[2] = $base[2] + $toadd[2];
     // Seconds
     while($res[2] >= 60) {
         $res[1] += 1;
         $res[2] -= 60;
     }
     // Minutes
     while($res[1] >= 60) {
         $res[0] += 1;
         $res[1] -= 60;
     }
     return implode(':', $res);
}

日期函数始终围绕/使用日/月/年。 你想要的是一个简单的数学函数,没有测试它,但应该弄清楚

private function add_time($base, $toadd) {
     $base = explode(':', $base);
     $toadd = explode(':', $toadd);

     $res = array();
     $res[0] = $base[0] + $toadd[0];
     $res[1] = $base[1] + $toadd[1];
     $res[2] = $base[2] + $toadd[2];
     // Seconds
     while($res[2] >= 60) {
         $res[1] += 1;
         $res[2] -= 60;
     }
     // Minutes
     while($res[1] >= 60) {
         $res[0] += 1;
         $res[1] -= 60;
     }
     return implode(':', $res);
}

下面是一个很好的小函数,它可以将数组中传递的任意次数相加:-

function addTimes(Array $times)
{
    $total = 0;
    foreach($times as $time){
        list($hours, $minutes, $seconds) = explode(':', $time);
        $hour = (int)$hours + ((int)$minutes/60) + ((int)$seconds/3600);
        $total += $hour;
    }
    $h = floor($total);
    $total -= $h;
    $m = floor($total * 60);
    $total -= $m/60;
    $s = floor($total * 3600);
    return "$h:$m:$s";
}
像这样使用它:-

$times = array('12:39:25', '08:22:10', '11:08:50', '07:33:05',);
var_dump(addTimes($times));
输出:-

string '39:43:30' (length=8)

下面是一个很好的小函数,它可以将数组中传递的任意次数相加:-

function addTimes(Array $times)
{
    $total = 0;
    foreach($times as $time){
        list($hours, $minutes, $seconds) = explode(':', $time);
        $hour = (int)$hours + ((int)$minutes/60) + ((int)$seconds/3600);
        $total += $hour;
    }
    $h = floor($total);
    $total -= $h;
    $m = floor($total * 60);
    $total -= $m/60;
    $s = floor($total * 3600);
    return "$h:$m:$s";
}
像这样使用它:-

$times = array('12:39:25', '08:22:10', '11:08:50', '07:33:05',);
var_dump(addTimes($times));
输出:-

string '39:43:30' (length=8)

这对于我试图做的事情来说效果更好,因为它们通常是我要添加的7倍,所有这些都已经在一个数组中了,这样我就可以运行另一个foreach循环:-。感谢这对我尝试做的工作更有效,因为它们通常是我要添加的7倍,所有这些都已经在一个数组中-省去了我运行另一个foreach循环:-。谢谢