当前位置:首页 > CMS教程 > 其它CMS > 列表

laravel实现按时间日期进行分组统计方法示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-11-14 10:20:32 浏览: 评论:0 

这篇文章主要给大家介绍了关于laravel如何实现按时间日期进行分组统计的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用laravel具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。

按日期进行分组

  1. //统计七天内注册用户数量按天进行分组 
  2. $user = DB::table('users')->whereBetween('created_at',['2018-01-01','2018-01-07']) 
  3.  ->selectRaw('date(created_at) as date,count(*) as value') 
  4.  ->groupBy('date')->get(); 
  5.  
  6. #获取的用户分组数据 
  7. { 
  8.  "date": "2018-01-01", #日期 
  9.  "value": 199  #数量 
  10. { 
  11.  "date": "2018-01-02", 
  12.  "value": 298 
  13. }, 
  14. { 
  15.  "date": "2018-01-03", 
  16.  "value": 1000 
  17. } 
  18.    
  19. #在进行图表统计的时候直接从数据库取得数据有些日期可能是没有的,就需要我们手动进行补全一些日期 
  20. #计算日期内天数 
  21. $stimestamp = strtotime($start_time); 
  22. $etimestamp = strtotime($end_time); 
  23. #计算日期段内有多少天 
  24. $days = ($etimestamp - $stimestamp) / 86400; 
  25. #保存每天日期 
  26. $date = array(); 
  27. for($i = 0;$i < $days;$i++){ 
  28.  $date[] = date('Y-m-d', $stimestamp + (86400 * $i)); 
  29. } 
  30. #循环补全日期 
  31. foreach ($date as $key => $val){ 
  32.  $data[$key] = [ 
  33.  'date' => $val, 
  34.  'value' => 0 
  35.  ]; 
  36.  foreach ($user as $item => $value){ 
  37.  if($val == $value['date']){ 
  38.   $data[$key] = $value; 
  39.  } 
  40.  } 
  41. } 
  42. return $data; 

按月份进行分组

  1. #统计一年内注册用户数量按月份进行分组 
  2. $user = DB::table('users')->whereBetween('created_at',['2018-01-01','2018-12-31']) 
  3.  ->selectRaw('DATE_FORMAT(created_at,"%Y-%m") as date,COUNT(*) as value') 
  4.  ->groupBy('date')->get(); 
  5. #获取的用户分组数据 
  6. { 
  7.  "date": "2018-01", #月份 
  8.  "value": 1497  #数量 
  9. }, 
  10. { 
  11.  "date": "2018-02", 
  12.  "value": 2354 
  13. }, 
  14. { 
  15.  "date": "2018-03", 
  16.  "value": 4560 
  17. }  
  18. #在进行图表统计的时候直接从数据库取得的数据有的月份可能是没有的,不过月份比较少可直接写死,同样也需要补全 
  19. $year = date('Y',time()); 
  20. #一年的月份 
  21. $month = [ 
  22.  0 => $year.'-01', 
  23.  1 => $year.'-02', 
  24.  2 => $year.'-03', 
  25.  3 => $year.'-04', 
  26.  4 => $year.'-05', 
  27.  5 => $year.'-06', 
  28.  6 => $year.'-07', 
  29.  7 => $year.'-08', 
  30.  8 => $year.'-09', 
  31.  9 => $year.'-10', 
  32.  10 => $year.'-11', 
  33.  11 => $year.'-12', 
  34. ]; 
  35. #循环补全月份 
  36. foreach ($month as $key => $val){ 
  37.  $data[$key] = [ 
  38.  'date' => $val, 
  39.  'value' => 0 
  40.  ]; 
  41.  foreach ($user as $item => $value){ 
  42.  if($val == $value['date']){ 
  43.   $data[$key] = $value; 
  44.  } 
  45.  } 
  46. } 
  47. return $data; 

laravel实现各时间段数量统计、方便直接使用

因项目中用到了图表之类的信息,需要获取到很多时间的数据动态,刚开始我都是自己换算时间来计算,后来 看到手册中有更简单的方法,自己总结了一下通用的时间段统计(今天、昨天、上周、本周、上月、本月、上年、本年)。

  1. use Carbon\Carbon; 
  2.    
  3. public function getNumber() 
  4. { 
  5.   $data = []; 
  6.  
  7.   #今天数据 
  8.   $data['customer_today'] = Customer::where('customer_type', 1)->where('created_at', Carbon::today())->count(); 
  9.   #昨天数据 
  10.   $data['customer_yesterday'] = Customer::where('customer_type', 1)->where('created_at', Carbon::yesterday())->count(); 
  11.  
  12.   // 本周数据 
  13.   $this_week = [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]; 
  14.   $data['customer_this_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $this_week)->count(); 
  15.  
  16.   // 上周数据 
  17.   $last_week = [Carbon::now()->startOfWeek()->subWeek(), Carbon::now()->endOfWeek()->subWeek()]; 
  18.   $data['customer_last_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $last_week)->count(); 
  19.  
  20.   // 本月数据 
  21.   $data['customer_this_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->month)->count(); 
  22.  
  23.   // 上月数据 
  24.   $data['customer_last_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count(); 
  25.  
  26.   // 本年数据 
  27.   $data['customer_this_year'] = Customer::where('customer_type', 1)->whereYear('created_at', Carbon::now()->year)->count(); 
  28.  
  29.     
  30.   return $data; 
  31. }

Tags: laravel时间日期 laravel分组统计

分享到: