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

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

$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`
 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统计一段时间间隔的数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Laravel timestamps 设置为unix时间戳的方法

    Laravel 修改 timestamps 为 unix 时间戳 <?php namespace App; use Illuminate\Database\Eloquent\Model; /** * Class Test * @package App */ class Test extends Model { /** * @var string */ protected $table='test'; /** * @var array */ protected $fillable = [ 'nam

  • laravel 时间格式转时间戳的例子

    数据渲染到模板经常用到日期格式.而数据库一般保存时间戳.每次更新或查询都要做转换. 使用Eloquent 自动转换  <?php namespace App\Model; use Illuminate\Database\Eloquent\Model; class Goods extends Model { public function setStartTimeAttribute($value) { $this->attributes['start_time'] = is_int($value

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

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

  • 使用django的ORM框架按月统计近一年内的数据方法

    如下所示: # 计算时间 time = datetime.datetime.now() - relativedelta(years=1) # 获取近一年数据 one_year_data = Data.objects.filter(create_time__gte=time_ago) # 分组统计每个月的数据 count_res = one_year_data\ .annotate(year=ExtractYear('create_time'),month=ExtractMonth('create

  • PHP+MySQL实现对一段时间内每天数据统计优化操作实例

    本文实例讲述了PHP+MySQL实现对一段时间内每天数据统计优化操作.分享给大家供大家参考,具体如下: 在互联网项目中,对项目的数据分析必不可少.通常会统计某一段时间内每天数据总计变化趋势调整营销策略.下面来看以下案例. 案例 在电商平台中通常会有订单表,记录所有订单信息.现在我们需要统计某个月份每天订单数及销售金额数据从而绘制出如下统计图,进行数据分析. 订单表数据结构如下: order_id order_sn total_price enterdate 25396 A4E610E250C2D

  • Laravel 读取 config 下的数据方法

    Laravel的config下一般存放配置信息,可以通过config('key')方法获取指定的数据. 设置值可通过「点」式语法读取,其中包含要访问的文件名以及选项名称. 现在想读取\config\app.php文件的url,文件中数据为: 'url' => 'http://localhost', 获取方法: config('app.url') config('app.url', 'default_val') 如果找不到app.url,会返回默认返回第二个参数. 以上这篇Laravel 读取 c

  • Laravel框架实现redis集群的方法分析

    本文实例讲述了Laravel框架实现redis集群的方法.分享给大家供大家参考,具体如下: 在app/config/database.php中配置如下: 'redis' => array( 'cluster' => true, 'default' => array( 'host' => '172.21.107.247', 'port' => 6379, ), 'redis1' => array( 'host' => '172.21.107.248', 'port'

  • python类:class创建、数据方法属性及访问控制详解

    在Python中,可以通过class关键字定义自己的类,然后通过自定义的类对象类创建实例对象. python中创建类 创建一个Student的类,并且实现了这个类的初始化函数"__init__": class Student(object):     count = 0     books = []     def __init__(self, name):         self.name = name 接下来就通过上面的Student类来看看Python中类的相关内容. 类构造和

  • Yii统计不同类型邮箱数量的方法

    本文实例讲述了Yii统计不同类型邮箱数量的方法.分享给大家供大家参考,具体如下: 效果图: 控制器: //查询邮箱 public function actionEmail() { /* //查询所有邮箱数据(1种) $arr=Users::find("select * from users")->asArray()->all(); //var_dump($data);die; $data=array(); //取出邮箱类型 foreach($arr as $key=>

  • mysql 找回误删表的数据方法(必看)

    有备份的话很简单,只需要生成一个最近备份的数据 然后用mysqlbinlog找回备份时间点之后的数据 再恢复到现网即可. 要是没有备份 可能就会比较麻烦,找回数据的成本也是非常之高的. 下面介绍下 mysqlbinlog找回备份时间点之后的数据的办法: 做个简单的实验,将mysql的表数据删除之后,然后用mysqlbinlog 找回刚才删除的表的数据. app表的创建时间和数据的插入: 2013-02-04 10:00:00 原理: mysqlbinlog 前提: mysql开启了bin log

  • Laravel实现自定义错误输出内容的方法

    本文实例讲述了Laravel实现自定义错误输出内容的方法.分享给大家供大家参考,具体如下: 这里分析一下laravel对于提交的数据进行验证,怎么自定义错误输出的内容 在根目录下运行命令 php artisan make:request PostUpdateRequest 会在app\Http\Requests目录下创建PostUpdateRequest文件 比如我设置 public function rules() { return [ 'posts_title' => 'required',

  • 使用coverage统计python web项目代码覆盖率的方法详解

    本文实例讲述了使用coverage统计python web项目代码覆盖率的方法.分享给大家供大家参考,具体如下: 在使用python+selenium过程中,有时候考虑代码覆盖率,所以专门查了一下python的coverage,所以特此记录 1.安装coverage 自己电脑安装了pip的   直接: pip install coverage,等待安装完成 安装完成后,会在C:\Python27\Scripts下看到相关的安装信息: 2.安装完成以后,就是开始使用了 2.1核心参数---run

随机推荐