繁体   English   中英

如何使用Laravel和Carbon获得过去6个月基于给定日期的每个星期内的所有结果

[英]How to get all results that fall under each weeks based on given date for the past 6 months using laravel and carbon

我每天创建许多记录,并且我想知道基于创建日期每周完成多少记录。 查询可能是这样的

$records_weekly = DB::table('records')

   ->where('created_at', '>=', Carbon::now()->subMonths(4))

   ->groupBy('week')

   ->get()

   ->toArray();

我希望你有口才:

$date = Carbon::now();

$records_weekly = Records::all()->where('created_at', '>=', Carbon::now()->subMonths(4))
                            ->groupBy(function($date) {
                                return Carbon::parse($date->created_at)->format('W');
                            })->sortKeysDesc();

这将产品输出一样,注意202122是周数

{
    "20": [...],
    "21": [...],
    "22": [
           {
            "id": 19,
            .....
            "created_at": "2018-05-31 07:46:33",
            "updated_at": "2018-05-23 07:46:33"
           }
          ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM