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 header()函数使用说明
Jul 10 PHP
php array_pop()数组函数将数组最后一个单元弹出(出栈)
Jul 12 PHP
PHP图片处理类 phpThumb参数用法介绍
Mar 11 PHP
php页面缓存ob系列函数介绍
Oct 18 PHP
你可能不知道PHP get_meta_tags()函数
May 12 PHP
PHP中的Streams详细介绍
Nov 12 PHP
php轻量级的性能分析工具xhprof的安装使用
Aug 12 PHP
php使用get_class_methods()函数获取分类的方法
Jul 20 PHP
php字符串操作针对负值的判断分析
Jul 28 PHP
php中请求url的五种方法总结
Jul 13 PHP
PHP文件管理之实现网盘及压缩包的功能操作
Sep 20 PHP
php探针不显示内存解决方法
Sep 17 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调用dll的实例操作动画与代码分享
2012/08/14 PHP
php生成数组的使用示例 php全组合算法
2014/01/16 PHP
Laravel 5框架学习之模型、控制器、视图基础流程
2015/04/08 PHP
PHP读书笔记整理_结构语句详解
2016/07/01 PHP
JQuery 自定义CircleAnimation,Animate方法学习笔记
2011/07/10 Javascript
基于jQuery实现Accordion手风琴自定义插件
2020/10/13 Javascript
基于JavaScript实现屏幕滚动效果
2017/01/18 Javascript
Jquery EasyUI $.Parser
2017/06/02 jQuery
浅谈webpack打包生成的bundle.js文件过大的问题
2018/02/22 Javascript
Angular Renderer (渲染器)的具体使用
2018/05/03 Javascript
微信小程序全局变量GLOBALDATA的定义和调用过程解析
2019/09/23 Javascript
浅谈vue项目用到的mock数据接口的两种方式
2019/10/09 Javascript
vue 移动端记录页面浏览位置的方法
2020/03/11 Javascript
微信小程序实现点击导航标签滚动定位到对应位置
2020/11/19 Javascript
python以环状形式组合排列图片并输出的方法
2015/03/17 Python
对Python进行数据分析_关于Package的安装问题
2017/05/22 Python
Python对象属性自动更新操作示例
2018/06/15 Python
pytorch训练imagenet分类的方法
2018/07/27 Python
python找出完数的方法
2018/11/12 Python
详解python列表生成式和列表生成式器区别
2019/03/27 Python
python 正则表达式参数替换实例详解
2020/01/17 Python
python tkinter GUI绘制,以及点击更新显示图片代码
2020/03/14 Python
python绘制分布折线图的示例
2020/09/24 Python
基于Python模拟浏览器发送http请求
2020/11/06 Python
基于HTML5 的人脸识别活体认证的实现方法
2016/06/22 HTML / CSS
HTML5 SEO优化的一些建议
2020/08/27 HTML / CSS
工程地质勘察专业大学生求职信
2013/10/13 职场文书
青年志愿者事迹材料
2014/02/07 职场文书
火车的故事教学反思
2014/02/11 职场文书
搞笑的获奖感言
2014/08/16 职场文书
竞聘报告优秀范文
2014/11/06 职场文书
2014年汽车销售工作总结
2014/12/01 职场文书
孟佩杰观后感
2015/06/17 职场文书
办公室规章制度范本
2015/08/04 职场文书
php 防护xss,PHP的防御XSS注入的终极解决方案
2021/04/01 PHP
MySQL数据库如何使用Shell进行连接
2022/04/12 MySQL