php小时份几段 整理数据

$min = 10*60; //秒

$start_date = strtotime("{$params['report_date']} 10:00:00");

$end_date = strtotime("{$params['report_date']} 23:00:00");

$list = array();

//整理出一个10分钟一段的数组

while (true) {

    $i = 0;

    $s = $start_date;

    $start_date = $start_date+$min;

    $list[] = ['s'=>$s,'e'=>$start_date];

    $i++;

    if($start_date>=$end_date) {

        break;

    }

}

$result = array();

//循环列表和时间段对比如果在时间段里面整理成新的数组,不再则是0

foreach ($rsList as  $key=>$value) {

    foreach($list as $k=>$v) {

        $a = strtotime($value['create_time']);

        $s = $v['s'];

        $e = $v['e'];

        if($s<=$a&&$a<=$e) {

            $result[date("H:i",$s)][] = ($value['user_average_value']);

            unset($rsList[$key]);

        }else{

            $result[date("H:i",$s)][] = '0';

            unset($rsList[$key]);

        }

}

}

$newData =[];

计算出有值并不重复的总个数,和值的合。整理成新的数组。

foreach ($result as $k=>$v){

    $count = count(array_filter($result[$k]));

    $sumval = array_sum($result[$k]);

    $val = sprintf("%.2f", $sumval/$count);

    $newData[$k] = $val;

}

你可能感兴趣的:(php小时份几段 整理数据)