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 Cookie的一个使用注意点
Nov 08 PHP
php 网页播放器用来播放在线视频的代码(自动判断并选择视频文件类型)
Jun 03 PHP
php正则表达式(regar expression)
Sep 10 PHP
PHP输入流php://input介绍
Sep 18 PHP
PHP处理excel cvs表格的方法实例介绍
May 13 PHP
360通用php防护代码(使用操作详解)
Jun 18 PHP
php数据类型判断函数有哪些
Sep 23 PHP
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单
May 15 PHP
php打造智能化的柱状图程序,用于报表等
Jun 19 PHP
yii实现使用CUploadedFile上传文件的方法
Dec 28 PHP
Laravel SQL语句记录方式(推荐)
May 26 PHP
php实现评论回复删除功能
May 23 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
亚洲咖啡有什么?亚洲咖啡产地介绍 亚洲咖啡有什么特点?
2021/03/05 新手入门
PHP 事件机制(2)
2011/03/23 PHP
php的urlencode()URL编码函数浅析
2011/08/09 PHP
ucenter中词语过滤原理分析
2016/07/13 PHP
YII2框架中日志的配置与使用方法实例分析
2020/03/18 PHP
Array, Array Constructor, for in loop, typeof, instanceOf
2011/09/13 Javascript
js 通过html()及text()方法获取并设置p标签的显示值
2014/05/14 Javascript
javascript实现显示和隐藏div方法汇总
2015/08/14 Javascript
js面向对象之常见创建对象的几种方式(工厂模式、构造函数模式、原型模式)
2015/11/09 Javascript
jquery实现邮箱自动填充提示功能
2015/11/17 Javascript
同步异步动态引入js文件的几种方法总结
2016/09/23 Javascript
学习JS中的DOM节点以及操作
2018/04/30 Javascript
Vue组件之单向数据流的解决方法
2018/11/10 Javascript
如何解决.vue文件url引用文件的问题
2019/01/18 Javascript
js中实例与对象的区别讲解
2019/01/21 Javascript
Vue路由前后端设计总结
2019/08/06 Javascript
jQuery zTree插件使用简单教程
2019/08/16 jQuery
微信小程序中data-key属性之数据传输(经验总结)
2020/08/22 Javascript
[00:58]2016年国际邀请赛勇士令状宣传片
2016/06/01 DOTA
python实现逆波兰计算表达式实例详解
2015/05/06 Python
Python中的单行、多行、中文注释方法
2018/07/19 Python
opencv与numpy的图像基本操作
2019/03/08 Python
pandas取出重复数据的方法
2019/07/04 Python
python爬虫项目设置一个中断重连的程序的实现
2019/07/26 Python
Python3使用PySynth制作音乐的方法
2019/09/09 Python
python中的逆序遍历实例
2019/12/25 Python
Python实现子类调用父类的初始化实例
2020/03/12 Python
巴基斯坦购物网站:Goto
2019/03/11 全球购物
Theo + George官方网站:都柏林时尚品牌
2019/04/08 全球购物
小学教师听课制度
2014/02/01 职场文书
毕业设计说明书
2014/05/07 职场文书
护校行动方案
2014/05/31 职场文书
结对共建协议书
2014/08/20 职场文书
群众路线教育党员自我剖析材料
2014/10/06 职场文书
创业计划书之珠宝饰品
2019/08/26 职场文书
MySQL数据库 安全管理
2022/05/06 MySQL