广告位联系
返回顶部
分享到

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

php 来源:互联网搜集 作者:秩名 发布时间:2019-10-09 20:13:42 人浏览
摘要

获取七天以前到现在的数据: $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(

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

$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')
 ]);



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`

{
 $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'));

效果图:




版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/zhezhebie/article/details/78622534
相关文章
  • PHP数据加密方式的总结

    PHP数据加密方式的总结
    首先我们来了解一下为什么要加密? 在网络通信的过程中攻击者可以伪造请求和返回,从而达到不可告人的目的。如下图所示: 数据加密之
  • PHP四种统计在线人数方式介绍

    PHP四种统计在线人数方式介绍
    1 用表统计方式 用数据表统计在线人数,这种方式只能用在并发量不大的情况下。 首先我们先新建表:user_login 编辑 user_login 表 模拟用户登
  • PHP获取系统毫秒数时间方法
    前言 php中获取时间方法是date(),在php中获取时间戳方法有time()、strtotime(); date():date(format, timestamp),format为格式、timestamp为时间戳(可选
  • PHP中的DI依赖注入的详细介绍
    什么是 DI / 依赖注入 依赖注入DI 其实本质上是指对类的依赖通过构造器完成 自动注入 通俗来说,就是你当前操作一个类,但是这个类的某
  • PHP8.1 Fiber交叉执行多任务(附代码)
    拿平时大家写的 for 循环举例。像 go 你可以写两个go每个里面各写一个循环同时输入,你可以看到输出是交替。在过去的php版本中,如果只开
  • PHP8.0的编译安装与使用的介绍
    安装与配置 本次使用的操作系统Ubuntu 18.04.4 LTS 安装 1.准备必要库 1 2 apt-get install -y autoconf libxml2-dev libsqlite3-dev \ libcurl4-openssl-dev libssl-dev l
  • Mac如何编译PHP 8.0 到MxSrvs工具

    Mac如何编译PHP 8.0 到MxSrvs工具
    开始准备工作 下载 PHP 8.0 PHP 官方下载 https://www.php.net/downloads.php 进入到 MxSrvs 的主程序路径下的/Applications/MxSrvs/bin,根据 Mxsrvs 的命名规则,
  • PHP8 中的 JIT的详细介绍

    PHP8 中的 JIT的详细介绍
    PHP 8 的 JIT(Just In Time)编译器将作为扩展集成到 php 中 Opcache 扩展 用于运行时将某些操作码直接转换为从 cpu 指令。 这意味着使用JIT后,
  • PHP8.2不再支持字符串中用${}插入变量了

    PHP8.2不再支持字符串中用${}插入变量了
    PHP 社区 4 月底通过了一项只有一张反对票的提案,提案内容是在即将发布的 PHP 8.2 中,不再支持使用 ${} 在字符串中插入变量的语法(标记
  • PHP8.2两个新的强类型:null和false的详细介绍

    PHP8.2两个新的强类型:null和false的详细介绍
    PHP 从 7.0 开始不断地在完善强类型,我们可以给方法参数、返回值、类属性等声明类型。 强类型可以让代码更加健壮,易于维护,可读性增
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计