有一个时间对应温度数组,然后根据自定义时间段获取该时间段平均温度,用laravel优雅的显示出来

管理员 发布于 3年前   300

需求:

1.每天时间与温度对应的一个有一个数组;(已有)

['2020-11-09 00:00:00' => 0,

'2020-11-09 01:00:00' => 0,

'2020-11-09 02:00:00' => -1,

'2020-11-09 03:00:00' => -1,

'2020-11-09 04:00:00' => -2,

'2020-11-09 05:00:00' => -2,

...]

2.用户自定义的时间段 比如 4点到8点/8点到11点等等;

3.求出每个不同时间段,平均温度值!(注意:优雅);


代码:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
class TestController extends Controller
{
    public function __invoke(Request $request)
    {
        $dates = [
            ['2020-11-09 04:00:00', '2020-11-09 08:00:00'],
            ['2020-11-09 08:00:00', '2020-11-09 11:00:00'],
            ['2020-11-09 11:00:00', '2020-11-09 15:00:00'],
            ['2020-11-09 15:00:00', '2020-11-09 22:00:00'],
            ['2020-11-09 22:00:00', '2020-11-10 04:00:00'],
            ['2020-11-10 04:00:00', '2020-11-10 08:00:00'],
            ['2020-11-10 08:00:00', '2020-11-12 11:00:00'],
            ['2020-11-10 11:00:00', '2020-11-12 15:00:00'],
        ];
        return Collection::make($dates)->map(function ($val) {
            $avg = $this->getAvgItem($val[0], $val[1])->avg();
            return [
                'start' => $val[0],
                'end' => $val[1],
                'avg' => $avg
            ];
        });
    }
    protected function getAvgItem(string $start, string $end)
    {
        $start_date = Carbon::parse($start);
        $end_date = Carbon::parse($end);
        $data = $this->getTemperature();
        return Collection::make($data)->filter(function ($key, $val) use ($start_date, $end_date) {
            $now_date = Carbon::parse($val);
            return $now_date->gte($start_date) && $now_date->lte($end_date);
        });
    }
    protected function getTemperature()
    {
        return [
            '2020-11-09 00:00:00' => 0,
            '2020-11-09 01:00:00' => 0,
            '2020-11-09 02:00:00' => -1,
            '2020-11-09 03:00:00' => -1,
            '2020-11-09 04:00:00' => -2,
            '2020-11-09 05:00:00' => -2,
            '2020-11-09 06:00:00' => -2,
            '2020-11-09 07:00:00' => -3,
            '2020-11-09 08:00:00' => -3,
            '2020-11-09 09:00:00' => 0,
            '2020-11-09 10:00:00' => 3,
            '2020-11-09 11:00:00' => 6,
            '2020-11-09 12:00:00' => 8,
            '2020-11-09 13:00:00' => 11,
            '2020-11-09 14:00:00' => 13,
            '2020-11-09 15:00:00' => 13,
            '2020-11-09 16:00:00' => 12,
            '2020-11-09 17:00:00' => 12,
            '2020-11-09 18:00:00' => 9,
            '2020-11-09 19:00:00' => 7,
            '2020-11-09 20:00:00' => 6,
            '2020-11-09 21:00:00' => 6,
            '2020-11-09 22:00:00' => 6,
            '2020-11-09 23:00:00' => 4,
            '2020-11-10 00:00:00' => 2,
            '2020-11-10 01:00:00' => 2,
            '2020-11-10 02:00:00' => 1,
            '2020-11-10 03:00:00' => 1,
            '2020-11-10 04:00:00' => 0,
            '2020-11-10 05:00:00' => 0,
            '2020-11-10 06:00:00' => 0,
            '2020-11-10 07:00:00' => 0,
            '2020-11-10 08:00:00' => 0,
            '2020-11-10 09:00:00' => 8,
            '2020-11-10 10:00:00' => 7,
            '2020-11-10 11:00:00' => 10,
            '2020-11-10 12:00:00' => 11,
            '2020-11-10 13:00:00' => 12,
            '2020-11-10 14:00:00' => 13,
            '2020-11-10 15:00:00' => 13,
        ];
    }
}


总结:基于laravel集合极其优雅的显示出来


请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章标签

友情链接

HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群
侯体宗的博客