Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP average time from an array

Tags:

php

time

How do I work out the average time from an array of times.

I have an array that looks like this :

('17:29:53','16:00:32')

And I wish to achieve the result 16:45:12 using PHP.

like image 974
user1217917 Avatar asked Feb 29 '12 09:02

user1217917


2 Answers

date('H:i:s', array_sum(array_map('strtotime', $array)) / count($array))

Untested solution typed on my phone, should work though.

like image 168
deceze Avatar answered Sep 19 '22 12:09

deceze


$times = array('17:29:53','16:00:32');

$totaltime = '';
foreach($times as $time){
        $timestamp = strtotime($time);
        $totaltime += $timestamp;
}

$average_time = ($totaltime/count($times));

echo date('H:i:s',$average_time);
like image 31
Crinsane Avatar answered Sep 16 '22 12:09

Crinsane