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

Laravel统计一段时间间隔的数据方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-12-28 20:18:25 浏览: 评论:0 

今天小编就为大家分享一篇Laravel统计一段时间间隔的数据方法,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。

获取七天以前到现在的数据:

  1. $days = Input::get('days', 7); 
  2.  
  3. $range = \Carbon\Carbon::now()->subDays($days); 
  4.  
  5. $stats = User::where('created_at', '>=', $range) 
  6.  ->groupBy('date') 
  7.  ->orderBy('date', 'DESC') 
  8.  ->get([ 
  9.   DB::raw('Date(created_at) as date'), 
  10.   DB::raw('COUNT(*) as value') 
  11.  ]); 

Laravel统计时间间隔

  1. SELECT  
  2.  
  3. sum(case when `EmailSource`='FM' then 1 else 0 end) as FM_Statistic, 
  4. sum(case when `EmailSource`='UOC' then 1 else 0 end) as UOC_Statistic, 
  5. sum(case when `EmailSource`='OC' then 1 else 0 end) as OC_Statistic, 
  6. DATE_FORMAT(Date,'%Y-%m-%d') AS `DateTime`  
  7. FROM `user_performance`  
  8. WHERE Email != '' AND Email != 'TOTAL' 
  9. AND (DATE_FORMAT(Date,'%Y-%m-%d') >= DATE_FORMAT('2011-02-5','%Y-%m-%d'))  
  10. AND (DATE_FORMAT(Date,'%Y-%m-%d') <= DATE_FORMAT('2011-03-07','%Y-%m-%d'))  
  11. GROUP BY `Date` 
  12.  
  13. public function getNumber() 
  14. { 
  15.  $data = []; 
  16.  $customers = Customer::all(['id', 'customer_type', 'created_at']); 
  17.  
  18.  #今天数据 
  19.  $data['customer_today'] = Customer::where('customer_type', 1)->where('created_at', Carbon::today())->count(); 
  20.  $data['teacher_today'] = Customer::where('customer_type', 2)->where('created_at', Carbon::today())->count(); 
  21.  
  22.  #昨天数据 
  23.  $data['customer_yesterday'] = Customer::where('customer_type', 1)->where('created_at', Carbon::yesterday())->count(); 
  24.  $data['teacher_yesterday'] = Customer::where('customer_type', 2)->where('created_at', Carbon::yesterday())->count(); 
  25.  
  26.  $data['today'] = $data['customer_today'] + $data['teacher_today']; 
  27.  $data['yesterday'] = $data['customer_yesterday'] + $data['teacher_yesterday']; 
  28.  
  29.  // 本周数据 
  30.  $this_week = [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]; 
  31.  $data['customer_this_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $this_week)->count(); 
  32.  
  33.  $data['teacher_this_week'] = Customer::where('customer_type', 2)->whereBetween('created_at', $this_week)->count(); 
  34.  
  35.  // 上周数据 
  36.  $last_week = [Carbon::now()->startOfWeek()->subWeek(), Carbon::now()->endOfWeek()->subWeek()]; 
  37.  $data['customer_last_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $last_week)->count(); 
  38.  
  39.  $data['teacher_last_week'] = Customer::where('customer_type', 2)->whereBetween('created_at', $last_week)->count(); 
  40.  
  41.  $data['this_week'] = $data['customer_this_week'] + $data['teacher_this_week']; 
  42.  $data['last_week'] = $data['customer_last_week'] + $data['teacher_last_week']; 
  43.  
  44.  // 本月数据 
  45.  $data['customer_this_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->month)->count(); 
  46.  $data['teacher_this_month'] = Customer::where('customer_type', 2)->whereMonth('created_at', Carbon::now()->month)->count(); 
  47.  
  48.  // 上月数据 
  49.  $data['customer_last_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count(); 
  50.  $data['teacher_last_month'] = Customer::where('customer_type', 2)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count(); 
  51.  
  52.  $data['this_month'] = $data['customer_this_month'] + $data['teacher_this_month']; 
  53.  $data['last_month'] = $data['customer_last_month'] + $data['teacher_last_month']; 
  54.  
  55.  // 本年数据 
  56.  $data['customer_this_year'] = Customer::where('customer_type', 1)->whereYear('created_at', Carbon::now()->year)->count(); 
  57.  $data['teacher_this_year'] = Customer::where('customer_type', 2)->whereYear('created_at', Carbon::now()->year)->count(); 
  58.  
  59.  $data['today_login_users'] = LoginLog::whereDate('created_at', '=', Carbon::today()) 
  60.   ->groupBy('customer_id') 
  61.   ->orderBy('customer_id') 
  62.   ->count(); 
  63.  
  64.  $data['yesterday_login_users'] = LoginLog::whereDate('created_at', '=', Carbon::yesterday()) 
  65.   ->groupBy('customer_id') 
  66.   ->orderBy('customer_id') 
  67.   ->count(); 
  68.  
  69.  $data['this_month_login_users'] = LoginLog::whereMonth('created_at', Carbon::now()->month) 
  70.   ->groupBy('customer_id') 
  71.   ->orderBy('customer_id') 
  72.   ->count(); 
  73.  
  74.  $data['last_month_login_users'] = LoginLog::whereMonth('created_at', Carbon::now()->subMonth()->month) 
  75.   ->groupBy('customer_id') 
  76.   ->orderBy('customer_id') 
  77.   ->count(); 
  78.  
  79.  return $data; 
  80. } 
  81.  
  82. public function numberCount() 
  83. { 
  84.  $days = request('days', 7); 
  85.  
  86.  $range = Carbon::today()->subDays($days); 
  87.  
  88.  $day_stats = Customer::where('created_at', '>=', $range) 
  89.   ->groupBy('date') 
  90.   ->orderBy('date', 'DESC') 
  91.   ->get([ 
  92.    \DB::raw('DATE_FORMAT(created_at,\'%Y-%m-%d\') as date,SUM(CASE WHEN customer_type = 1 THEN 1 ELSE 0 END) AS customer,SUM(CASE WHEN customer_type = 2 THEN 1 ELSE 0 END) AS teacher'), 
  93.   ]) 
  94.   ->toJSON(); 
  95.  
  96.  $week_stats = Customer::groupBy('week') 
  97.   ->orderBy('week', 'DESC') 
  98.   ->get([ 
  99.    \DB::raw('DATE_FORMAT(created_at,\'%Y W%u\') as week,SUM(CASE WHEN customer_type = 1 THEN 1 ELSE 0 END) AS customer, SUM(CASE WHEN customer_type = 2 THEN 1 ELSE 0 END) AS teacher'), 
  100.   ]) 
  101.   ->toJSON(); 
  102.  // dd($week_stats); 
  103.  
  104.  // \DB::enableQueryLog(); 
  105.  $month_stats = Customer::groupBy('month') 
  106.   ->orderBy('month', 'DESC') 
  107.   ->get([ 
  108.    \DB::raw('DATE_FORMAT(created_at,\'%Y-%m\') as month,SUM(CASE WHEN customer_type = 1 THEN 1 ELSE 0 END) AS customer,SUM(CASE WHEN customer_type = 2 THEN 1 ELSE 0 END) AS teacher'), 
  109.   ]) 
  110.   ->toJSON(); 
  111.  // dd(\DB::getQueryLog()); 
  112.  // dd($week_stats, $month_stats); 
  113.  $data = $this->getNumber(); 
  114.  // dd($day_stats, $week_stats, $month_stats, $data); 
  115.  return view('admin.numberCount', compact('day_stats', 'week_stats', 'month_stats', 'data')); 
  116. } 

效果图:

Laravel统计时间间隔

Tags: Laravel统计时间间隔

分享到: