当前位置:首页 > 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 endas FM_Statistic, 
  4. sum(case when `EmailSource`='UOC' then 1 else 0 endas UOC_Statistic, 
  5. sum(case when `EmailSource`='OC' then 1 else 0 endas 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.  $data = []; 
  15.  $customers = Customer::all(['id''customer_type''created_at']); 
  16.  
  17.  #今天数据 
  18.  $data['customer_today'] = Customer::where('customer_type', 1)->where('created_at', Carbon::today())->count(); 
  19.  $data['teacher_today'] = Customer::where('customer_type', 2)->where('created_at', Carbon::today())->count(); 
  20.  
  21.  #昨天数据 
  22.  $data['customer_yesterday'] = Customer::where('customer_type', 1)->where('created_at', Carbon::yesterday())->count(); 
  23.  $data['teacher_yesterday'] = Customer::where('customer_type', 2)->where('created_at', Carbon::yesterday())->count(); 
  24.  
  25.  $data['today'] = $data['customer_today'] + $data['teacher_today']; 
  26.  $data['yesterday'] = $data['customer_yesterday'] + $data['teacher_yesterday']; 
  27.  
  28.  // 本周数据 
  29.  $this_week = [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]; 
  30.  $data['customer_this_week'] = Customer::where('customer_type', 1)->whereBetween('created_at'$this_week)->count(); 
  31.  
  32.  $data['teacher_this_week'] = Customer::where('customer_type', 2)->whereBetween('created_at'$this_week)->count(); 
  33.  
  34.  // 上周数据 
  35.  $last_week = [Carbon::now()->startOfWeek()->subWeek(), Carbon::now()->endOfWeek()->subWeek()]; 
  36.  $data['customer_last_week'] = Customer::where('customer_type', 1)->whereBetween('created_at'$last_week)->count(); 
  37.  
  38.  $data['teacher_last_week'] = Customer::where('customer_type', 2)->whereBetween('created_at'$last_week)->count(); 
  39.  
  40.  $data['this_week'] = $data['customer_this_week'] + $data['teacher_this_week']; 
  41.  $data['last_week'] = $data['customer_last_week'] + $data['teacher_last_week']; 
  42.  
  43.  // 本月数据 
  44.  $data['customer_this_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->month)->count(); 
  45.  $data['teacher_this_month'] = Customer::where('customer_type', 2)->whereMonth('created_at', Carbon::now()->month)->count(); 
  46.  
  47.  // 上月数据 
  48.  $data['customer_last_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count(); 
  49.  $data['teacher_last_month'] = Customer::where('customer_type', 2)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count(); 
  50.  
  51.  $data['this_month'] = $data['customer_this_month'] + $data['teacher_this_month']; 
  52.  $data['last_month'] = $data['customer_last_month'] + $data['teacher_last_month']; 
  53.  
  54.  // 本年数据 
  55.  $data['customer_this_year'] = Customer::where('customer_type', 1)->whereYear('created_at', Carbon::now()->year)->count(); 
  56.  $data['teacher_this_year'] = Customer::where('customer_type', 2)->whereYear('created_at', Carbon::now()->year)->count(); 
  57.  
  58.  $data['today_login_users'] = LoginLog::whereDate('created_at''=', Carbon::today()) 
  59.   ->groupBy('customer_id'
  60.   ->orderBy('customer_id'
  61.   ->count(); 
  62.  
  63.  $data['yesterday_login_users'] = LoginLog::whereDate('created_at''=', Carbon::yesterday()) 
  64.   ->groupBy('customer_id'
  65.   ->orderBy('customer_id'
  66.   ->count(); 
  67.  
  68.  $data['this_month_login_users'] = LoginLog::whereMonth('created_at', Carbon::now()->month) 
  69.   ->groupBy('customer_id'
  70.   ->orderBy('customer_id'
  71.   ->count(); 
  72.  
  73.  $data['last_month_login_users'] = LoginLog::whereMonth('created_at', Carbon::now()->subMonth()->month) 
  74.   ->groupBy('customer_id'
  75.   ->orderBy('customer_id'
  76.   ->count(); 
  77.  
  78.  return $data
  79.  
  80. public function numberCount() 
  81.  $days = request('days', 7); 
  82.  
  83.  $range = Carbon::today()->subDays($days); 
  84.  
  85.  $day_stats = Customer::where('created_at''>='$range
  86.   ->groupBy('date'
  87.   ->orderBy('date''DESC'
  88.   ->get([ 
  89.    \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'), 
  90.   ]) 
  91.   ->toJSON(); 
  92.  
  93.  $week_stats = Customer::groupBy('week'
  94.   ->orderBy('week''DESC'
  95.   ->get([ 
  96.    \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'), 
  97.   ]) 
  98.   ->toJSON(); 
  99.  // dd($week_stats); 
  100.  
  101.  // \DB::enableQueryLog(); 
  102.  $month_stats = Customer::groupBy('month'
  103.   ->orderBy('month''DESC'
  104.   ->get([ 
  105.    \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'), 
  106.   ]) 
  107.   ->toJSON(); 
  108.  // dd(\DB::getQueryLog()); 
  109.  // dd($week_stats, $month_stats); 
  110.  $data = $this->getNumber(); 
  111.  // dd($day_stats, $week_stats, $month_stats, $data); 
  112.  return view('admin.numberCount', compact('day_stats''week_stats''month_stats''data')); 

效果图:

Laravel统计时间间隔

Tags: Laravel统计时间间隔

分享到: