查询时忽略秒数 created_at
Ignore seconds when querying created_at
我有一个计划任务,每 5 分钟 运行 会在我 运行.
的服务器上收集一些统计数据
在等待请求返回时有一个小的延迟,因此记录总是在 2 或 3 秒后保存。即任务 运行s 在 2017-14-03 08:00:00
,但记录保存在 2017-14-03 08:00:03
。
我正在尝试提取记录以显示在图表上。图表会缩放到您想要查看的时间段(通过使用新数据刷新图表的硬编码按钮)。
我尝试做的第一个图表是过去 24 小时内的图表。我不想在过去 24 小时内每 5 分钟返回一次,我只想每小时返回一次。我已经构建了一个函数来向下舍入到最近的小时,然后以此为基础获取最后 24 小时 - 它看起来像这样:
public function last24Hours()
{
$times = [];
$time = Carbon::now()->minute(0)->second(0);
$i = 1;
while($i <= 24)
{
array_push($times, $time->toDateTimeString());
$time->subHour();
$i++;
}
return $times;
}
使用返回的时间,我正在尝试使用 whereIn()
查询模型,如下所示:
$stats = ServerTracking::whereIn('created_at', $this->last24Hours())->get();
查询 运行s,但没有任何结果 - 因为 created_at
时间与我查询的时间相差几秒钟。
我遇到了一些障碍,想不出解决这个问题的方法?有什么想法吗?
本身不是解决方案,但我会采取不同的方法。假设我知道你正在尝试查询最近 24 小时(1 天),我会做
$now = Carbon::now();
$stats = ServerTracking::where('created_at', '<=', $now) //now
->where('created_at', '>=', $now->subHours(24)) //24hours ago
->get();
使用 whereBetween 类似,但更短一些
$now = Carbon::now();
$stats = ServerTracking::whereBetween('created_at', [$now, $now->subHours(24)])
->get();
您可以将 selectRaw 与格式化日期一起使用:
$stats = ServerTracking::selectRaw('foo,bar,DATE_FORMAT(created_at, "%Y-%m-%d %H:00:00") as hour_created')->get()->keyBy('hour_created');
每个小时的所有值都将具有相同的 hour_created
,keyBy 将只保留其中一个(来自文档):
If multiple items have the same key, only the last one will appear in the new collection.
只需将 foo 和 bar 替换为您需要的其他值即可。您将保留 0:55 分钟值或 0:00 分钟值,具体取决于您对查询进行排序的方式。
想想看,您可以使用 whereRaw 按您的方式进行操作:
->whereRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:00:00') in (" .implode(",",$last24Hours). ")")
我有一个计划任务,每 5 分钟 运行 会在我 运行.
的服务器上收集一些统计数据在等待请求返回时有一个小的延迟,因此记录总是在 2 或 3 秒后保存。即任务 运行s 在 2017-14-03 08:00:00
,但记录保存在 2017-14-03 08:00:03
。
我正在尝试提取记录以显示在图表上。图表会缩放到您想要查看的时间段(通过使用新数据刷新图表的硬编码按钮)。
我尝试做的第一个图表是过去 24 小时内的图表。我不想在过去 24 小时内每 5 分钟返回一次,我只想每小时返回一次。我已经构建了一个函数来向下舍入到最近的小时,然后以此为基础获取最后 24 小时 - 它看起来像这样:
public function last24Hours()
{
$times = [];
$time = Carbon::now()->minute(0)->second(0);
$i = 1;
while($i <= 24)
{
array_push($times, $time->toDateTimeString());
$time->subHour();
$i++;
}
return $times;
}
使用返回的时间,我正在尝试使用 whereIn()
查询模型,如下所示:
$stats = ServerTracking::whereIn('created_at', $this->last24Hours())->get();
查询 运行s,但没有任何结果 - 因为 created_at
时间与我查询的时间相差几秒钟。
我遇到了一些障碍,想不出解决这个问题的方法?有什么想法吗?
本身不是解决方案,但我会采取不同的方法。假设我知道你正在尝试查询最近 24 小时(1 天),我会做
$now = Carbon::now();
$stats = ServerTracking::where('created_at', '<=', $now) //now
->where('created_at', '>=', $now->subHours(24)) //24hours ago
->get();
使用 whereBetween 类似,但更短一些
$now = Carbon::now();
$stats = ServerTracking::whereBetween('created_at', [$now, $now->subHours(24)])
->get();
您可以将 selectRaw 与格式化日期一起使用:
$stats = ServerTracking::selectRaw('foo,bar,DATE_FORMAT(created_at, "%Y-%m-%d %H:00:00") as hour_created')->get()->keyBy('hour_created');
每个小时的所有值都将具有相同的 hour_created
,keyBy 将只保留其中一个(来自文档):
If multiple items have the same key, only the last one will appear in the new collection.
只需将 foo 和 bar 替换为您需要的其他值即可。您将保留 0:55 分钟值或 0:00 分钟值,具体取决于您对查询进行排序的方式。
想想看,您可以使用 whereRaw 按您的方式进行操作:
->whereRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:00:00') in (" .implode(",",$last24Hours). ")")