I have an array like so
array (size=5107)
0 => int 421
1 => int 996
2 => int 1555
3 => int 399
4 => int 57914
5 => int 57245
6 => int 7176
7 => int 7166
8 => int 5987
This array is being generated by pulling some timestamps from a database, comparing them and then getting the difference in seconds like so;
$start_date = new DateTime($startdate);
$since_start = $start_date->diff(new DateTime($closedate));
$diff = strtotime($closedate) - strtotime($startdate);
I then want to get the average of the array, but whenever I do a array_sum($array);
the result is always negative and I'm not sure why. What am I misunderstanding here?
If I change
$diff = strtotime($closedate) - strtotime($startdate);
To
$diff = strtotime($startdate) - strtotime($closedate);
The array_sum($array);
results in a positive value but all the array values are negative but all the results are the same.
You should get the absolute value:
$diff = abs(strtotime($closedate) - strtotime($startdate));