laravel按日期时间分组并统计

  • 统计七天内注册用户数量按天进行分组
$user = DB::table('users')->whereBetween('created_at',['2018-01-01','2018-01-07'])
        ->selectRaw('DATE(created_at) as date,COUNT(*) as value')
        ->groupBy('date')->get();
  • 统计一年内注册用户数量按月份进行分组
$user = DB::table('users')->whereBetween('created_at',['2018-01-01','2018-12-31'])
        ->selectRaw('DATE_FORMAT(created_at,"%Y-%m") as date,COUNT(*) as value')
        ->groupBy('date')->get();
    原文作者:渡目成书
    原文地址: https://blog.csdn.net/weixin_43674113/article/details/106116656
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞