1

How to get sum of all total_duration in CodeIgniter.

Array
(
    [caller] => Ravneet Kaur
    [total_calls] => 1
    [total_duration] => 00:00:06
)

Array
(
    [caller] => Navneet Kaur
    [total_calls] => 2
    [total_duration] => 00:00:19
)

Array
(
    [caller] => Jasmeet Kaur
    [total_calls] => 0
    [total_duration] => 00:00:00
)

Array
(
    [caller] => sunny Kumar
    [total_calls] => 0
    [total_duration] => 00:00:00
)
David
  • 1,147
  • 4
  • 17
  • 29
Ruhanika
  • 29
  • 6

2 Answers2

0

This answer may help to solve your issue with the help of array_column

Check this:

$total = array_column($array, 'total_duration'); 
$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);
Community
  • 1
  • 1
David
  • 1,147
  • 4
  • 17
  • 29
0

Here it is sum of duration

$seconds= 0;
foreach ($new_arr as $key => $value) { 
   list($hour,$minute,$second) = explode(':',$value["total_duration"]);                                               
   $seconds += $hour*3600;
   $seconds += $minute*60;
   $seconds += $second;

}           

$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes  = floor($seconds/60);
$seconds -= $minutes*60;

echo  sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);

you can check answer here => https://eval.in/592792

Hope this answer will help you

Divyesh Patoriya
  • 518
  • 3
  • 15
Mehul Kuriya
  • 608
  • 5
  • 18