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 相关文章推荐
海河写的 Discuz论坛帖子调用js的php代码
Aug 23 PHP
php email邮箱正则
Oct 08 PHP
攻克CakePHP系列一 连接MySQL数据库
Oct 22 PHP
在mysql数据库原有字段后增加新内容
Nov 26 PHP
在php中判断一个请求是ajax请求还是普通请求的方法
Jun 28 PHP
php 获取本地IP代码
Jun 23 PHP
关于php程序报date()警告的处理(date_default_timezone_set)
Oct 22 PHP
php使用array_search函数实现数组查找的方法
Jun 12 PHP
php把时间戳转换成多少时间之前函数的实例
Nov 16 PHP
PHP 使用二进制保存用户状态的实例
Jan 29 PHP
php在windows环境下获得cpu内存实时使用率(推荐)
Feb 08 PHP
PHP代码覆盖率统计详解
Jul 22 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
MOTOROLA 摩托罗拉 MODEL 66-XI五灯中波收音机
2021/03/02 无线电
PHP 各种排序算法实现代码
2009/08/20 PHP
php面向对象全攻略 (七) 继承性
2009/09/30 PHP
屏蔽机器人从你的网站搜取email地址的php代码
2012/11/14 PHP
ThinkPHP中使用ajax接收json数据的方法
2014/12/18 PHP
PHP中文编码小技巧
2014/12/25 PHP
PHP Opcache安装和配置方法介绍
2015/05/28 PHP
获取网站跟路径的javascript代码(站点及虚拟目录)
2009/10/20 Javascript
屏蔽Flash右键信息的js代码
2010/01/17 Javascript
Javascript 网页水印(非图片水印)实现代码
2010/03/01 Javascript
jquery autocomplete自动完成插件的的使用方法
2010/08/07 Javascript
js中单引号与双引号冲突问题解决方法
2013/10/04 Javascript
js动态控制table的tr、td增加及删除的具体实现
2014/04/30 Javascript
IE下通过a实现location.href 获取referer的值
2014/09/04 Javascript
Spring mvc 接收json对象
2015/12/10 Javascript
JavaScript中removeChild 方法开发示例代码
2016/08/15 Javascript
微信小程序 同步请求授权的详解
2017/08/04 Javascript
基于vue-cli npm run build之后vendor.js文件过大的解决方法
2018/09/27 Javascript
使用 node.js 模仿 Apache 小部分功能
2019/07/07 Javascript
CentOS6.5设置Django开发环境
2016/10/13 Python
Python表示矩阵的方法分析
2017/05/26 Python
Python数据结构之图的应用示例
2018/05/11 Python
Python实现的银行系统模拟程序完整案例
2019/04/12 Python
python中的协程深入理解
2019/06/10 Python
VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法详解
2019/07/01 Python
Pytorch实现神经网络的分类方式
2020/01/08 Python
Python+redis通过限流保护高并发系统
2020/04/15 Python
利用Python实现字幕挂载(把字幕文件与视频合并)思路详解
2020/10/21 Python
Manuka Doctor美国官网:麦卢卡蜂蜜和蜂毒护肤
2016/12/25 全球购物
What is EJB
2016/07/22 面试题
客服部班长工作责任制
2014/02/25 职场文书
2014年五四青年节活动方案
2014/03/29 职场文书
城管综合整治方案
2014/05/01 职场文书
活动宣传策划方案
2014/05/23 职场文书
搞笑婚前保证书
2015/02/28 职场文书
植树节新闻稿
2015/07/17 职场文书