I want to add and take an average of a few hours.
$cadena = '00:09:00,00:09:00,00:09:00,00:09:00,00:09:00';
$arr = explode(",",$cadena);
$total = strtotime('00:00:00');
for($i = 0; $i<count($arr);$i++)
{
$total = $total + strtotime($arr[$i]);
}
$total = $total / count($arr);
echo date('H:i:s',$total);
This returns me 05:45:00 and I expected 00:09:00. What was the mistake :(
You could do something like this:
When you call strtotime without passing it a date, it assumes the current date, and the timestamp of today plus nine minutes is not the same as the timestamp of T=0 plus nine minutes.
In particular, timestamp 0 occurred on December 31, 1969 at 9:00 PM, so there is an implied offset of three hours in any strtotime. That has consequences as strange as that
print
In your case, you're initializing the total to a non-zero value, so averaging that sum becomes meaningless.
Iván Botero's solution solves your problem because it explicitly initializes the total to zero, and at each iteration of the loop it removes the time delta between the current date and the initial timestamp of the universe.
if you want to add here is this I hope it helps