我想获得平均时间,但我遇到了问题,因为如果我使用DateTime.add()对象在超过24小时时添加时间,则会将时间转换为“天”。基本上我只想在“小时”内得到这些时间的平均值。请帮忙。
$time = array();
$time[] = '00:00:58';
$time[] = '00:45:08';
$time[] = '00:00:49';
$time[] = '00:11:26';
$time[] = '02:34:39';
$time[] = '02:18:24';
$time[] = '02:24:11';
$time[] = '01:23:19';
$time[] = '00:27:30';
$time[] = '02:14:50';
$time[] = '00:48:47';
$time[] = '02:19:52';
$time[] = '02:04:29';
$time[] = '01:08:46';
$time[] = '00:06:57';
$time[] = '00:35:31';
$time[] = '02:16:59';
$time[] = '02:23:33';
$time[] = '02:36:30';
$time[] = '02:11:47';
$time[] = '02:40:44';
$time[] = '02:03:42';
$time[] = '01:45:50';
答案 0 :(得分:1)
试试这个,
$total_time=0;
foreach($time as $t){
$total_time+=strtotime($t)-1373241600;
}
所以$ total_time将是一个整数,您可以将其除以3600和计数($ time)以获得平均小时:)
$average_time = ($total_time/count($time))/3600;
答案 1 :(得分:0)
试试这个(注意闭合在5.3 +中可用):
$timestamps = array_map(function($a){return strtotime($a);}, $time);
$avg = (array_sum($timestamps)/count($timestamps))/3600;
答案 2 :(得分:0)
如果您不想依赖时区设置,减去当前时间的时间戳或者因为strtotime的迟缓而烦恼,请使用简单的数学:
$timestamps = array_map(function($item){
list($h, $m, $s) = explode(':', $item);
return intval($h) * 3600 + intval($m) * 60 + intval($s);
}, $time);
$avg = ceil(array_sum($timestamps)/count($timestamps));
echo gmdate('H:i:s', $avg);