Sum(ADD)在php中超过两个时间值

时间:2014-12-29 06:47:21

标签: php

$time = array("18:10:00", "23:10:12", "10:05:00");

如何从此阵列获取总时间。我需要输出像51:25:12,请帮帮我

6 个答案:

答案 0 :(得分:3)

试试这个 短代码:

它填补了你的所有情况。例如:$a=array("18:30:00", "23:30:12", "10:05:00");

function sum_time($array) {
    $i = 0;
    foreach ($array as $time) {
        sscanf($time, '%d:%d:%d', $hour, $min,$sec);
        $i += ($hour * 60 + $min)*60+$sec;
    }
    if ($h = floor($i / 3600)) {
        $i %= 3600;
        if ($m = floor($i / 60)) {
            $i %= 60;
        }
    }
    return sprintf('%02d:%02d:%02d', $h, $m,$i);
}
$a=array("18:30:00", "23:30:12", "10:05:00");
echo sum_time($a);

答案 1 :(得分:1)

// sample data
$time = array("18:50:00", "23:10:12", "10:05:00");

//variable initialization
$seconds = $mins = $hours = array();

//loop through all sample data items
foreach($time as $tk => $tv) {

  //explode each item with seperator
  $tv_parts = explode(":", $tv);

  $seconds[] = $tv_parts['2'];
  $mins[] = $tv_parts['1'];
  $hours[] = $tv_parts['0'];
}

//add up all items respectively
$ts = array_sum($seconds);
$tm = array_sum($mins);
$th = array_sum($hours);

//adjust seconds if they are more than 59
if($ts > 59) {
  $ts = $ts % 60;
  $tm = $tm + floor($ts / 60);
}

//adjust minutes if they are more than 59
if($tm > 59) {
  $tm = $tm % 60;
  $th = $th + floor($tm / 60);
}

//padding for adjusting it to two digits when sum is below 10
$th = str_pad($th, 2, "0", STR_PAD_LEFT);
$tm = str_pad($tm, 2, "0", STR_PAD_LEFT);
$ts = str_pad($ts, 2, "0", STR_PAD_LEFT);

//final output
echo "$th:$tm:$ts";

您可以在PHP官方文档网站上提及有关array_sumfloorstr_pad的更多详细信息。

答案 2 :(得分:0)

最简单的方法如下:

$time = array("18:10:00", "23:10:12", "10:05:00");
$sum="00:00:00";
$sum_new = explode(':',$sum);
foreach ($time as $t)
{
    $time_new = explode(':',$t);
    $sum_new[0]=$sum_new[0]+$time_new[0];
    $sum_new[1]=$sum_new[1]+$time_new[1];
    $sum_new[2]=$sum_new[2]+$time_new[2];
}
$sum = implode(':',$sum_new);
echo $sum;

答案 3 :(得分:0)

首先通过:爆炸当前日期字符串,而不仅仅是总结部分。不要忘记修复时间部分的溢出:

function sum($times) {
    $total = array(
        'h' => 0,
        'm' => 0,
        's' => 0,
    );

    foreach ($times as $t) {
        $timeArray = explode(":", $t);
        $total['h'] += $timeArray[0];
        $total['m'] += $timeArray[1];
        $total['s'] += $timeArray[2];
    }

    if ($total['s'] >= 60) {
        $total['m'] += $total['s'] % 60;
        $intpart = floor($total['s']);
        $total['s'] = $total['s'] - $intpart;
    }

    if ($total['m'] >= 60) {
        $total['h'] += $total['m'] % 60;
        $intpart = floor($total['m']);
        $total['m'] = $total['m'] - $intpart;
    }

    return $total;
}

$totals = sum(array("18:10:00", "23:10:12", "10:05:00"));
echo implode(':', $totals);

答案 4 :(得分:0)

试试这个:

<?php

$time = array("18:10:00", "23:10:12", "10:05:00");
$seconds = 0;
foreach($time as $t)
{
$timeArr = array_reverse(explode(":", $t));

foreach ($timeArr as $key => $value)
{
    if ($key > 2) break;
    $seconds += pow(60, $key) * $value;
}

}

$hours = floor($seconds / 3600);
$mins = floor(($seconds - ($hours*3600)) / 60);
$secs = floor($seconds % 60);

echo $hours.':'.$mins.':'.$secs;

答案 5 :(得分:0)

<?php
    $time = array("18:10:00", "23:10:12", "10:05:00");
     $hours=0;
     $min=0;
     $sec=0;
       foreach($time as $time_array)
    {
       $time_exp=explode(':',$time_array); 
       $hours=$hours+$time_exp[0];
       $min=$min+$time_exp[1];
       $sec=$sec+$time_exp[2];


    }
     $time_output='';
     $time_output=$hours.':'.$min.':'.$sec;
     echo $time_output; 

?>