Quite a challenge. 😉 I don't know of any PHP functions that can be used to perform math functions with time (it's possible there are some already), so I'll do it the hard way. Here are some of the tool's we'll use:
Here's the code (all nice and tested):
<?php
print '<pre>';
$times = array("4:35", "5:45", "12:53");
//Convert the $times values to arrays, split by minutes and seconds.
//This results in a multidimensional array (which you can see with print_r($times);)
foreach($times as $key =>$value)
{
$times[$key] = explode(":", $value);
}
//This section will add up the times
$tminutes = 0;
$tseconds = 0;
foreach($times as $value)
{
$tminutes += $value[0];
$tseconds += $value[1];
}
//This line will find out how many minutes are in the total seconds
//i.e. in 310 seconds there are 5 minutes.
$minutes = intval($tseconds/60);
//This line adds the minutes (from the previous line) to the total.
$tminutes += $minutes;
//This line will remove the minutes from the total seconds
//i.e. With 310 seconds, there are 5 minutes and 10 seconds
//So, $tseconds becomes 10
$tseconds -= ($minutes*60);
//At this point, we have the total time in minutes and seconds.
//Now we want to average the times.
//Essentially, we'll use the same process as above.
$laps = count($times);
$average = (($tminutes*60 + $tseconds)/$laps);
$aminutes = 0;
$aseconds = $average;
$minutes = intval($average/60);
$aminutes += $minutes;
$aseconds -= ($minutes*60);
$aseconds = intval($aseconds);
//Now we have the average time in minutes and seconds.
//This will print the total and average times in human-readable format.
print "The total time for the run was " . $tminutes . ":" . $tseconds . '.<br />';
print "There were $laps laps with an average of " . $aminutes . ":" . $aseconds . ' per lap.';
print '</pre>';
?>
In case you're wondering, the <pre> tags are for debugging. Oh, and here's the output:
The total time for the run was 23:13.
There were 3 laps with an average of 7:44 per lap.