有一个时间对应温度数组,然后根据自定义时间段获取该时间段平均温度,用laravel优雅的显示出来
Laravel  /  管理员 发布于 2年前   716
需求:
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集合极其优雅的显示出来
路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..博主 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..mashrdn 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 V2rayn免费节点添加上去了,youtobe无法打开网页,是怎么回事..张伟 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 3q!有用,不过免费节点隔天就要去git上复制新的导进去..博主 在
科学上网翻墙访问Google , 上外网神器佛跳墙VPN(永久免费)使用流程步骤中评论 该篇教程已不能用了,告知大家,免的老有老铁问我!..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号