laravel定时脚本(laravel实现按时间日期进行分组统计方法示例)
类别:编程学习 浏览量:2697
时间:2022-04-02 01:39:07 laravel定时脚本
laravel实现按时间日期进行分组统计方法示例按日期进行分组
|
//统计七天内注册用户数量按天进行分组 $user = DB::table( 'users' )->whereBetween( 'created_at' ,[ '2018-01-01' , '2018-01-07' ]) ->selectRaw( 'date(created_at) as date,count(*) as value' ) ->groupBy( 'date' )->get(); #获取的用户分组数据 { "date" : "2018-01-01" , #日期 "value" : 199 #数量 { "date" : "2018-01-02" , "value" : 298 }, { "date" : "2018-01-03" , "value" : 1000 } #在进行图表统计的时候直接从数据库取得数据有些日期可能是没有的,就需要我们手动进行补全一些日期 #计算日期内天数 $stimestamp = strtotime ( $start_time ); $etimestamp = strtotime ( $end_time ); #计算日期段内有多少天 $days = ( $etimestamp - $stimestamp ) / 86400; #保存每天日期 $date = array (); for ( $i = 0; $i < $days ; $i ++){ $date [] = date ( 'Y-m-d' , $stimestamp + (86400 * $i )); } #循环补全日期 foreach ( $date as $key => $val ){ $data [ $key ] = [ 'date' => $val , 'value' => 0 ]; foreach ( $user as $item => $value ){ if ( $val == $value [ 'date' ]){ $data [ $key ] = $value ; } } } return $data ; |
按月份进行分组
|
#统计一年内注册用户数量按月份进行分组 $user = DB::table( 'users' )->whereBetween( 'created_at' ,[ '2018-01-01' , '2018-12-31' ]) ->selectRaw( 'DATE_FORMAT(created_at,"%Y-%m") as date,COUNT(*) as value' ) ->groupBy( 'date' )->get(); #获取的用户分组数据 { "date" : "2018-01" , #月份 "value" : 1497 #数量 }, { "date" : "2018-02" , "value" : 2354 }, { "date" : "2018-03" , "value" : 4560 } #在进行图表统计的时候直接从数据库取得的数据有的月份可能是没有的,不过月份比较少可直接写死,同样也需要补全 $year = date ( 'Y' ,time()); #一年的月份 $month = [ 0 => $year . '-01' , 1 => $year . '-02' , 2 => $year . '-03' , 3 => $year . '-04' , 4 => $year . '-05' , 5 => $year . '-06' , 6 => $year . '-07' , 7 => $year . '-08' , 8 => $year . '-09' , 9 => $year . '-10' , 10 => $year . '-11' , 11 => $year . '-12' , ]; #循环补全月份 foreach ( $month as $key => $val ){ $data [ $key ] = [ 'date' => $val , 'value' => 0 ]; foreach ( $user as $item => $value ){ if ( $val == $value [ 'date' ]){ $data [ $key ] = $value ; } } } return $data ; |
laravel实现各时间段数量统计、方便直接使用
因项目中用到了图表之类的信息,需要获取到很多时间的数据动态,刚开始我都是自己换算时间来计算,后来 看到手册中有更简单的方法,自己总结了一下通用的时间段统计(今天、昨天、上周、本周、上月、本月、上年、本年)。
|
use Carbon\Carbon; public function getNumber() { $data = []; #今天数据 $data [ 'customer_today' ] = Customer::where( 'customer_type' , 1)->where( 'created_at' , Carbon::today())-> count (); #昨天数据 $data [ 'customer_yesterday' ] = Customer::where( 'customer_type' , 1)->where( 'created_at' , Carbon::yesterday())-> count (); // 本周数据 $this_week = [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]; $data [ 'customer_this_week' ] = Customer::where( 'customer_type' , 1)->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 [ 'customer_this_month' ] = Customer::where( 'customer_type' , 1)->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 [ 'customer_this_year' ] = Customer::where( 'customer_type' , 1)->whereYear( 'created_at' , Carbon::now()->year)-> count (); return $data ; } |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对开心学习网的支持。
您可能感兴趣
- php获取数组中和为指定数的数组(php统计数组不同元素的个数的实例方法)
- php递归实现(PHP递归统计系统中代码行数)
- python统计图参数(Python使用统计函数绘制简单图形实例代码)
- mysql数值类型图解(详解mysql持久化统计信息)
- dedecms如何更改文章发布时间(Dedecms实现自动统计当前栏目文档总数的方法)
- pandas统计多行数据(pandas去重复行并分类汇总的实现方法)
- vue统计代码行数(vue实现计数器简单制作)
- laravel请求耗时(Laravel统计一段时间间隔的数据方法)
- python群聊提示(python-itchat 统计微信群、好友数量,及原始消息数据的实例)
- python怎么判断文件大小(python3实现指定目录下文件sha256及文件大小统计)
- c#中使用stopwatch统计代码执行时间
- dede发布不了文章(dede中统计栏目文章数的2种实现方法)
- dede数据统计(DEDE [field:global name=autoindex/] 按序号排列从0从1开始)
- python中统计一个字符出现的次数(Python统计一个字符串中每个字符出现了多少次的方法字符串转换为列表再统计)
- python统计出现文字最多的词(使用Python 统计高频字数的方法)
- group by是底层如何实现分组的(group by 按某一时间段分组统计并查询推荐)
- 泰国旅游攻略(泰国旅游攻略必去景点)
- 数字藏品市场有多乱 周杰伦丢了 一只猴 ,损失超300万(数字藏品市场有多乱)
- 这里输入关键词(怎么输入关键词搜索)
- 得这个 难治病 的人太多了,300个人赶到杭州商量怎么办(得这个难治病的人太多了)
- 经度,世界时间腕表的灵魂(世界时间腕表的灵魂)
- 阿里最新财报公布 三季度营收增长3 ,将增加150亿美元回购额度 在美股价小涨(阿里最新财报公布)
热门推荐
- 使用mui制作页面案例(Yii框架页面渲染操作实例详解)
- laravel自定义使用方法(Laravel使用Queue队列的技巧汇总)
- docker启动springboot项目(Docker运行springboot项目的实现)
- js和css哪个重要(js与css的阻塞问题详析)
- vue走马灯特效(Javascript结合Vue实现对任意迷宫图片的自动寻路)
- vue各组件如何引用(Vue局部组件数据共享Vue.observable的使用)
- sql server 分布式事务(Sql Server事务语法及使用方法实例分析)
- docker镜像如何打包(Docker打包镜像及修改配置的实现)
- dede模板手机端显示设置(DEDE模板中如何运行php脚本和变量在需要操作数据库字段时)
- 100道python真实面试题附答案(值得收藏的10道python 面试题)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9