濮阳杆衣贸易有限公司

主頁 > 知識庫 > laravel實現按時間日期進行分組統(tǒng)計方法示例

laravel實現按時間日期進行分組統(tǒng)計方法示例

熱門標簽:阿里云400電話申請加工單 西區(qū)企業(yè)怎么做地圖標注入駐 七魚外呼系統(tǒng)停用嗎 海南人工外呼系統(tǒng)有效果嗎 保定crm外呼系統(tǒng)運營商 九江外呼系統(tǒng) 智能電話機器人排名前十名南京 抖音有個地圖標注是什么意思 地下城堡2圖九地圖標注

按日期進行分組

//統(tǒng)計七天內注冊用戶數量按天進行分組
$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
}
 
#在進行圖表統(tǒng)計的時候直接從數據庫取得數據有些日期可能是沒有的,就需要我們手動進行補全一些日期
#計算日期內天數
$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));
}
#循環(huán)補全日期
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;

按月份進行分組

#統(tǒng)計一年內注冊用戶數量按月份進行分組
$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
} 
#在進行圖表統(tǒng)計的時候直接從數據庫取得的數據有的月份可能是沒有的,不過月份比較少可直接寫死,同樣也需要補全
$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',
];
#循環(huán)補全月份
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實現各時間段數量統(tǒng)計、方便直接使用

因項目中用到了圖表之類的信息,需要獲取到很多時間的數據動態(tài),剛開始我都是自己換算時間來計算,后來 看到手冊中有更簡單的方法,自己總結了一下通用的時間段統(tǒng)計(今天、昨天、上周、本周、上月、本月、上年、本年)。

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;
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • Laravel5.1框架路由分組用法實例分析
  • Laravel 實現Eloquent模型分組查詢并返回每個分組的數量 groupBy()
  • laravel 實現劃分admin和home 模塊分組
  • 解決laravel groupBy 對查詢結果進行分組出現的問題
  • Laravel框架中的路由和控制器操作實例分析
  • Laravel框架路由和控制器的綁定操作方法
  • Laravel 5框架學習之路由、控制器和視圖簡介
  • laravel框架分組控制器和分組路由實現方法示例

標簽:昭通 遼陽 梅河口 韶關 九江 涼山 甘肅 十堰

巨人網絡通訊聲明:本文標題《laravel實現按時間日期進行分組統(tǒng)計方法示例》,本文關鍵詞  laravel,實現,按,時間,日期,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《laravel實現按時間日期進行分組統(tǒng)計方法示例》相關的同類信息!
  • 本頁收集關于laravel實現按時間日期進行分組統(tǒng)計方法示例的相關信息資訊供網民參考!
  • 推薦文章
    方山县| 内黄县| 佳木斯市| 惠水县| 永川市| 茂名市| 古丈县| 广宁县| 泰宁县| 仪陇县| 剑川县| 京山县| 枝江市| 砚山县| 扎兰屯市| 信宜市| 板桥市| 通河县| 民勤县| 招远市| 毕节市| 广西| 浮山县| 正镶白旗| 湘阴县| 三河市| 江陵县| 前郭尔| 高清| 都江堰市| 班玛县| 赞皇县| 永登县| 陆良县| 丹凤县| 南充市| 青阳县| 商洛市| 普陀区| 莒南县| 揭阳市|