gpt4 book ai didi

php - Laravel 按自定义日期字段分组

转载 作者:行者123 更新时间:2023-11-29 20:28:21 27 4
gpt4 key购买 nike

我有超过 16,000 strip 有相应日期的数据。我将这些日期放在表格的“日期”字段中。我想按月份显示这些数据,我尝试过但失败了,我的日期格式: date: "2015-08-31 02:12 "

我的查询:

$data = \App\Crawler::select([
\DB::raw('count(id) as `count`'),
\DB::raw('Month(date) as month')
])->groupBy('month')->get();

我想要:

{
{
month: july
count: 100
},
{
month: march
count: 100
}
}

最佳答案

您应该能够使用 MySQL 的 MONTHNAME() 函数获取月份名称。

即使您没有存储完整的时间戳,这也应该有效。

$data = \App\Crawler::select([
\DB::raw('count(id) as `count`'),
\DB::raw('monthname(date) as month')
])->groupBy('month')->get();

 

[
{
count: 3,
month: "July"
},
{
count: 1,
month: "March"
}
]

关于php - Laravel 按自定义日期字段分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39177154/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com