2

My array looks like this

Array
(
    [0] => Array
        (
            [ltime] => 00:02:55
        )

    [1] => Array
        (
            [ltime] => 00:07:56
        )

    [2] => Array
        (
            [ltime] => 01:03:32
        )   

    [3] => Array
        (
            [ltime] => 01:13:34
        )

    [4] => Array
        (
            [ltime] => 02:13:44
        ) 

    [5] => Array
        (
            [ltime] => 03:08:53
        )

    [6] => Array
        (
            [ltime] => 03:13:54
        )      
)

how do i calculate total hours [ltime] from this array. This is what i have tried

$sum = 0;
foreach($arr as $shift) {
    $start = explode(':', $shift[0]);
    $end = explode(':', $shift[1]);
    $sum += ($end[0] - $start[0]) + ($end[1] - $start[1]) / 100 / 0.6;
}

which does not work. How to achieve this.

EDIT

This is what exactly i want.

ltime1 is    00:02 
                    [5 min]   
ltime2 is    00:07 
                    [56 min]
ltime3 is    01:03
                    [10 min]    
ltime4 is    01:13
                    [1 hour]
ltime5 is    02:13
                    [55 min]
ltime6 is    03:08
                    [5 min]
ltime7 is    03:13
---------------------------------------
Total time          3 hours 11 minutes

This is actually finding time difference the sum all the values.

Matarishvan
  • 2,382
  • 3
  • 38
  • 68

5 Answers5

3

The given order of your data is low -> high. For calculating increasing time I have made it reverse or you can try other way too.

But the key thing is getting the time in common format - unix timestamp. We are using strtotime() for this. The rest are traversing calculating.

The following code gives the result you expected.

$data = array(
    array('ltime' => "00:02:55"),
    array('ltime' => "00:07:56"),
    array('ltime' => "01:03:32"),
    array('ltime' => "01:13:34"),
    array('ltime' => "02:13:44"),
    array('ltime' => "03:08:53"),
    array('ltime' => "03:13:54")
);

$data = array_reverse($data);

$prev = 0;
$diff = strtotime("00:00:00");

array_walk($data, function($item, $key) use (&$diff, &$prev){
    if ($prev == 0) {
        $prev = strtotime($item['ltime']);
    } else {
        $diff += ($prev - strtotime($item['ltime']));
        $prev = strtotime($item['ltime']);
    }

});

var_dump(date("H:i:s",$diff));
Himel Nag Rana
  • 744
  • 1
  • 11
  • 19
2

Have a look at below solution:

$total = [
    '00:02:55',
    '00:07:56',
    '01:03:32',
    '01:13:34',
    '02:13:44',
    '03:08:53',
    '03: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);

Output:

11:04:28

Above solution was for sum of time.

UPDATE

Refer below solution for desired result:

$x = null;
$sum = strtotime('00:00:00');
foreach($total as $t){
    $date = new DateTime($t);
    if($x){
        $interval = $date->diff($date2);
        echo "difference " . $interval->h . " hour, " . $interval->i." minutes, ".$interval->s." second";
        $sum1=strtotime($interval->h.':'.$interval->i.':'.$interval->s)-$sum;
        $sum2 = $sum2+$sum1;
        echo "<br />";
    }
    $date2 = $date;

    $x = 1;
}
$sum3=$sum+$sum2;

echo date("H:i:s",$sum3);

Output

difference 0 hour, 5 minutes, 1 second
difference 0 hour, 55 minutes, 36 second
difference 0 hour, 10 minutes, 2 second
difference 1 hour, 0 minutes, 10 second
difference 0 hour, 55 minutes, 9 second
difference 0 hour, 5 minutes, 1 second
03:10:59
Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
1

Try this function:

function totalTime($times=array()) {
     if(!is_array($times)) {
         $times = array($times);
     }
     $seconds = 0;
     foreach ($times as $time) {
       list($hour,$minute,$second) = explode(':', $time);
       $seconds += $hour*3600;
       $seconds += $minute*60;
       $seconds += $second;
     }
     $hours = floor($seconds / 3600);
     $seconds -= $hours * 3600;
     $minutes  = floor($seconds / 60);
     $seconds -= $minutes * 60;

     // return "{$hours}:{$minutes}:{$seconds}";
     return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}


$yourArray = array(...);

//clean your array, so each array value will be a duration

echo totalTime($yourArray);
Scriptlabs
  • 498
  • 3
  • 16
1

This is the basic script:

$tot = strtotime( '00:00:00' );
for( $i=1; $i<count( $array ); $i++ )
{
    $tot += strtotime( $array[$i]['ltime'] ) - strtotime( $array[$i-1]['ltime'] );
}

echo date( 'H:i:s', $tot );

will output:

03:10:59

or:

if( $str = date('G',$tot) ) echo "$str hours ";
if( $str = date('i',$tot) ) echo "$str minutes ";

will output:

3 hours 10 minutes

But you round down minutes, so you need this:

$tot = strtotime( '00:00:00' );
for( $i=1; $i<count( $array ); $i++ )
{
    $tot += strtotime( sprintf('%s:00',substr($array[$i]['ltime'],0,5) )) - strtotime( sprintf('%s:00',substr($array[$i-1]['ltime'],0,5) ));
}

if( $str = date('G',$tot) ) echo "$str hours ";
if( $str = date('i',$tot) ) echo "$str minutes ";

will output:

3 hours 11 minutes
fusion3k
  • 11,568
  • 4
  • 25
  • 47
0

You can do this much simpler, than the other guys suggest. Basically loop over your data array, convert your H:i:s time into seconds using strtotime() collect results and convert it to H:i:s format at the end.

<?php

$data = [
  '00:02:55',
  '00:07:56',
  '01:03:32',
  '01:13:34',
  '02:13:44',
  '03:08:53',
  '03:13:54'
];

$totalTime = 0;

foreach ($data as $time) {
    $totalTime = $totalTime + strtotime("1970-01-01 $time UTC");
}

var_dump($totalTime); // Total Seconds
echo "Total time: " . gmdate("H:i:s", $totalTime); // H:i:s format

Output

int(39868) 
Total time: 11:04:28

Fiddle

Tomasz
  • 4,847
  • 2
  • 32
  • 41