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的urlencode()URL编码函数浅析
Aug 09 PHP
PHP中函数rand和mt_rand的区别比较
Dec 26 PHP
PHP读取xml方法介绍
Jan 12 PHP
PHP代码优化之成员变量获取速度对比
Feb 28 PHP
PHP防范SQL注入的具体方法详解(测试通过)
May 09 PHP
[原创]php获取数组中键值最大数组项的索引值
Mar 17 PHP
关于PHP 如何用 curl 读取 HTTP chunked 数据
Feb 26 PHP
php入门教程之Zend Studio设置与开发实例
Sep 09 PHP
phpinfo()中Loaded Configuration File(none)的解决方法
Jan 16 PHP
yii2多图上传组件的使用教程
May 10 PHP
PHP中如何使用Redis接管文件存储Session详解
Nov 28 PHP
PHP实现获取文件mime类型多种方法解析
May 28 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中时间轴开发(刚刚、5分钟前、昨天10:23等)
2011/10/03 PHP
使用php 获取时间今天明天昨天时间戳的详解
2013/06/20 PHP
深入解析fsockopen与pfsockopen的区别
2013/07/05 PHP
PHP+MySQL删除操作实例
2015/01/21 PHP
关于IFRAME 自适应高度的研究
2006/07/20 Javascript
改善你的jQuery的25个步骤 千倍级效率提升
2010/02/11 Javascript
js Calender控件使用详解
2015/01/05 Javascript
javascript中eval解析JSON字符串
2016/02/27 Javascript
Bootstrap实现带暂停功能的轮播组件(推荐)
2016/11/25 Javascript
JS字符串按逗号和回车分隔的方法
2017/04/25 Javascript
jquery+ajax实现省市区三级联动 (封装和不封装两种方式)
2017/05/15 jQuery
vue jsx 使用指南及vue.js 使用jsx语法的方法
2017/11/11 Javascript
webpack多入口文件页面打包配置详解
2018/01/09 Javascript
layui扩展上传组件模拟进度条的方法
2019/09/23 Javascript
webpack的pitching loader详解
2019/09/23 Javascript
浅析JS中NEW的实现原理及重写
2020/02/20 Javascript
使用Python神器对付12306变态验证码
2016/01/05 Python
Python实现的密码强度检测器示例
2017/08/23 Python
如何使用 Pylint 来规范 Python 代码风格(来自IBM)
2018/04/06 Python
Python3爬取英雄联盟英雄皮肤大图实例代码
2018/11/14 Python
Python3的unicode编码转换成中文的问题及解决方案
2019/12/10 Python
python列表生成器迭代器实例解析
2019/12/19 Python
python实现简单井字棋游戏
2020/03/04 Python
解决python使用list()时总是报错的问题
2020/05/05 Python
python 中的命名空间,你真的了解吗?
2020/08/19 Python
西班牙太阳镜品牌:Hawkers
2018/03/11 全球购物
西班牙家用电器和电子产品购物网站:Mi Electro
2019/02/25 全球购物
重写子类方法时,抛出异常的书写注意事项
2015/10/17 面试题
小学三年级数学教学反思
2014/01/31 职场文书
优秀少先队大队辅导员事迹材料
2014/05/04 职场文书
婚礼秀策划方案
2014/05/19 职场文书
小学生关于梦想的演讲稿
2014/08/22 职场文书
2014年安全工作总结范文
2014/11/13 职场文书
2016教师校本研修心得体会
2016/01/08 职场文书
Spring-cloud Config Server的3种配置方式
2021/09/25 Java/Android
JS前端轻量fabric.js系列物体基类
2022/08/05 Javascript