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


Posted in PHP onOctober 09, 2019

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

$days = Input::get('days', 7);

$range = \Carbon\Carbon::now()->subDays($days);

$stats = User::where('created_at', '>=', $range)
 ->groupBy('date')
 ->orderBy('date', 'DESC')
 ->get([
  DB::raw('Date(created_at) as date'),
  DB::raw('COUNT(*) as value')
 ]);

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

SELECT 
sum(case when `EmailSource`='FM' then 1 else 0 end) as FM_Statistic,
sum(case when `EmailSource`='UOC' then 1 else 0 end) as UOC_Statistic,
sum(case when `EmailSource`='OC' then 1 else 0 end) as OC_Statistic,
DATE_FORMAT(Date,'%Y-%m-%d') AS `DateTime` 
FROM `user_performance` 
WHERE Email != '' AND Email != 'TOTAL'
AND (DATE_FORMAT(Date,'%Y-%m-%d') >= DATE_FORMAT('2011-02-5','%Y-%m-%d')) 
AND (DATE_FORMAT(Date,'%Y-%m-%d') <= DATE_FORMAT('2011-03-07','%Y-%m-%d')) 
GROUP BY `Date`
public function getNumber()
 {
  $data = [];
  $customers = Customer::all(['id', 'customer_type', 'created_at']);

  #今天数据
  $data['customer_today'] = Customer::where('customer_type', 1)->where('created_at', Carbon::today())->count();
  $data['teacher_today'] = Customer::where('customer_type', 2)->where('created_at', Carbon::today())->count();

  #昨天数据
  $data['customer_yesterday'] = Customer::where('customer_type', 1)->where('created_at', Carbon::yesterday())->count();
  $data['teacher_yesterday'] = Customer::where('customer_type', 2)->where('created_at', Carbon::yesterday())->count();

  $data['today'] = $data['customer_today'] + $data['teacher_today'];
  $data['yesterday'] = $data['customer_yesterday'] + $data['teacher_yesterday'];

  // 本周数据
  $this_week = [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()];
  $data['customer_this_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $this_week)->count();

  $data['teacher_this_week'] = Customer::where('customer_type', 2)->whereBetween('created_at', $this_week)->count();

  // 上周数据
  $last_week = [Carbon::now()->startOfWeek()->subWeek(), Carbon::now()->endOfWeek()->subWeek()];
  $data['customer_last_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $last_week)->count();

  $data['teacher_last_week'] = Customer::where('customer_type', 2)->whereBetween('created_at', $last_week)->count();

  $data['this_week'] = $data['customer_this_week'] + $data['teacher_this_week'];
  $data['last_week'] = $data['customer_last_week'] + $data['teacher_last_week'];

  // 本月数据
  $data['customer_this_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->month)->count();
  $data['teacher_this_month'] = Customer::where('customer_type', 2)->whereMonth('created_at', Carbon::now()->month)->count();

  // 上月数据
  $data['customer_last_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count();
  $data['teacher_last_month'] = Customer::where('customer_type', 2)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count();

  $data['this_month'] = $data['customer_this_month'] + $data['teacher_this_month'];
  $data['last_month'] = $data['customer_last_month'] + $data['teacher_last_month'];

  // 本年数据
  $data['customer_this_year'] = Customer::where('customer_type', 1)->whereYear('created_at', Carbon::now()->year)->count();
  $data['teacher_this_year'] = Customer::where('customer_type', 2)->whereYear('created_at', Carbon::now()->year)->count();

  $data['today_login_users'] = LoginLog::whereDate('created_at', '=', Carbon::today())
   ->groupBy('customer_id')
   ->orderBy('customer_id')
   ->count();

  $data['yesterday_login_users'] = LoginLog::whereDate('created_at', '=', Carbon::yesterday())
   ->groupBy('customer_id')
   ->orderBy('customer_id')
   ->count();

  $data['this_month_login_users'] = LoginLog::whereMonth('created_at', Carbon::now()->month)
   ->groupBy('customer_id')
   ->orderBy('customer_id')
   ->count();

  $data['last_month_login_users'] = LoginLog::whereMonth('created_at', Carbon::now()->subMonth()->month)
   ->groupBy('customer_id')
   ->orderBy('customer_id')
   ->count();

  return $data;
 }
public function numberCount()
 {
  $days = request('days', 7);

  $range = Carbon::today()->subDays($days);

  $day_stats = Customer::where('created_at', '>=', $range)
   ->groupBy('date')
   ->orderBy('date', 'DESC')
   ->get([
    \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'),
   ])
   ->toJSON();

  $week_stats = Customer::groupBy('week')
   ->orderBy('week', 'DESC')
   ->get([
    \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'),
   ])
   ->toJSON();
  // dd($week_stats);

  // \DB::enableQueryLog();
  $month_stats = Customer::groupBy('month')
   ->orderBy('month', 'DESC')
   ->get([
    \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'),
   ])
   ->toJSON();
  // dd(\DB::getQueryLog());
  // dd($week_stats, $month_stats);
  $data = $this->getNumber();
  // dd($day_stats, $week_stats, $month_stats, $data);
  return view('admin.numberCount', compact('day_stats', 'week_stats', 'month_stats', 'data'));
 }

效果图:

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

以上这篇Laravel统计一段时间间隔的数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
PHP 应用程序的安全 -- 不能违反的四条安全规则
Nov 26 PHP
粗略计算在线时间,bug:ip相同
Dec 09 PHP
基于pear auth实现登录验证
Feb 26 PHP
Thinkphp模板中截取字符串函数简介
Jun 17 PHP
ThinkPHP3.1基础知识快速入门
Jun 19 PHP
PHP实现伪静态方法汇总
Jan 13 PHP
注释PHP和html混合代码的小技巧(分享)
Nov 03 PHP
Windows平台实现PHP连接SQL Server2008的方法
Jul 26 PHP
PHP性能分析工具xhprof的安装使用与注意事项
Dec 19 PHP
PHP基于PDO扩展操作mysql数据库示例
Dec 24 PHP
PHP实现文字写入图片功能
Feb 18 PHP
PHP中echo与print区别点整理
Mar 09 PHP
浅谈PHP5.6 与 PHP7.0 区别
Oct 09 #PHP
laravel按天、按小时,查询数据的实例
Oct 09 #PHP
laravel多条件查询方法(and,or嵌套查询)
Oct 09 #PHP
Laravel find in set排序实例
Oct 09 #PHP
对laravel in 查询的使用方法详解
Oct 09 #PHP
laravel实现查询最后执行的一条sql语句的方法
Oct 09 #PHP
Laravel使用原生sql语句并调用的方法
Oct 09 #PHP
You might like
提升PHP执行速度全攻略(下)
2006/10/09 PHP
drupal 代码实现URL重写
2011/05/04 PHP
获取PHP警告错误信息的解决方法
2013/06/03 PHP
web server使用php生成web页面的三种方法总结
2013/10/28 PHP
PHP基于php_imagick_st-Q8.dll实现JPG合成GIF图片的方法
2014/07/11 PHP
PHP按指定键值对二维数组进行排序的方法
2015/12/22 PHP
Django中的cookie与session操作实例代码
2017/08/17 PHP
用JavaScript脚本实现Web页面信息交互
2006/10/11 Javascript
asp.net+jquery滚动滚动条加载数据的下拉控件
2010/06/25 Javascript
精通Javascript系列之数值计算
2011/06/07 Javascript
Javascript控制div属性动态变化实例分析
2015/10/08 Javascript
使用堆实现Top K算法(JS实现)
2015/12/25 Javascript
AngularJS实现Model缓存的方式
2016/02/03 Javascript
BootStrap glyphicons 字体图标实现方法
2016/05/01 Javascript
jQuery select自动选中功能实现方法分析
2016/11/28 Javascript
js遍历获取表格内数据的方法(必看)
2017/04/06 Javascript
Javascript中类式继承和原型式继承的实现方法和区别之处
2017/04/25 Javascript
nodejs中Express与Koa2对比分析
2018/02/06 NodeJs
vue加载自定义的js文件方法
2018/03/13 Javascript
详解Vue实战指南之依赖注入(provide/inject)
2018/11/13 Javascript
vue实现自定义多选按钮
2020/07/16 Javascript
[01:02:07]Liquid vs Newbee 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
[01:45]典藏宝瓶2+祈求者身心——这就是DOTA2TI9总奖金突破3000万美元的秘密
2019/07/21 DOTA
使用Python下的XSLT API进行web开发的简单教程
2015/04/15 Python
在Python的Django框架中编写编译函数
2015/07/20 Python
python代码 if not x: 和 if x is not None: 和 if not x is None:使用介绍
2016/09/21 Python
详解python中executemany和序列的使用方法
2017/08/12 Python
python放大图片和画方格实现算法
2018/03/30 Python
python 使用poster模块进行http方式的文件传输到服务器的方法
2019/01/15 Python
python多线程并发让两个LED同时亮的方法
2019/02/18 Python
用Python从0开始实现一个中文拼音输入法的思路详解
2019/07/20 Python
两年的个人工作自我评价
2014/01/10 职场文书
管理专员自荐信
2014/01/26 职场文书
团队队名口号大全
2014/06/06 职场文书
小学开学标语
2014/07/01 职场文书
市级绿色学校申报材料
2014/08/25 职场文书