1

I have an array,each element have a start_date and end_date.
I want to sum all the time periods between the two dates and divide them by 3 to get the average time period.

The Array

Array
 (
 [0] => Array
  (
  [start_date] => "2014-01-30 09:27:02"
  [end_date] => "2014-01-30 16:29:38"
  )
 [1] => Array
  (
  [start_date] => "2014-01-28 09:27:02"
  [end_date] => "2014-01-30 16:29:38"
  )
 [2] => Array
  (
  [start_date] => "2014-01-30 09:05:02"
  [end_date] => "2014-01-30 12:12:38"
  )
 )    

I need some thing like this:

$total=0; 
foreach($array as $ar)    
{   
    $created_dt=strtotime($ar[$start_date]); 
    $done_dt=strtotime($ar[$end_date]); 
    $runing_time= $created_dt - $done_dt - 2*3600;
    $runing_time= date('H:i',$runing_time);     
    $total+=$runing_time;    
}     
$runing_time=$total/3;    

what is a good way to do this?
thanks :)

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
user2706762
  • 397
  • 1
  • 3
  • 11

3 Answers3

1

Technically you are already correct.

But it order to have the given script to run propely you may have edit it to

$total=0; 
foreach($array as $ar)    
{   
    $created_dt=strtotime($ar['start_date']); 
    $done_dt=strtotime($ar['end_date']); 
    $runing_time= $created_dt - $done_dt - 2*3600;
    $runing_time= date('H.i',$runing_time);     
    $total+=$runing_time;    
}
$runing_time=$total/3;   

so I only changed 3 thing. first your $start_date and $end_date was an variable that was not existing.

and second if you want the average instead of : put . so it can be read as a double.

and if you want the variable $total to be the real value i would do this instead.

 $runing_time= date('H',$runing_time).'.'.date('i',$runing_time)/60*100;
Nicolas Racine
  • 1,031
  • 3
  • 13
  • 35
1

As shown at this answer it's ultra simple. :) (i have slightly modified it)

 $now = time(); // or your date as well
 $your_date = strtotime("2010-01-01");
 $datediff = $now - $your_date;
 $numberOfDays = floor($datediff/(60*60*24));

Now you just have to put it in foreach and sum in $numberOfDays in another variable.

And please remember to check if first date is lower than second date

Community
  • 1
  • 1
Mr.TK
  • 1,743
  • 2
  • 17
  • 22
1

You are making a fundamental mistake in trying to treat a time period as a date/time, you cannot use date() and its related functions for time periods.

The way to find the average of time periods is to sum the number of seconds and then divide that by the number of periods. The following function does this:-

function averageTime(array $times){
    $total = 0;
    foreach($times as $time){
        $total += (new \DateTime($time['end_date']))->getTimestamp() - (new \DateTime($time['start_date']))->getTimestamp();
    }
    $avSeconds = $total/count($times);
    $hours = floor($avSeconds/3600);
    $avSeconds -= $hours * 3600;
    $minutes = floor($avSeconds/60);
    $avSeconds -= $minutes * 60;
    $seconds = floor($avSeconds);
    return "{$hours}h {$minutes}m {$seconds}s";
}

Plugging your example array into it:-

$times= array(
    0 => Array(
        'start_date' => "2014-01-30 09:27:02",
        'end_date' => "2014-01-30 16:29:38",
    ),
    1 => Array(
        'start_date' => "2014-01-28 09:27:02",
        'end_date' => "2014-01-30 16:29:38",
    ),
    2 => Array(
        'start_date' => "2014-01-30 09:05:02",
        'end_date' => "2014-01-30 12:12:38",
    ),
);

echo "Average time = " . averageTime($times);

Output:-

Average time = 21h 44m 16s

Ref DateTime manual.

vascowhite
  • 18,120
  • 9
  • 61
  • 77