最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2019年03月23日 14:45:35   作者:王羽落  
這篇文章主要給大家介紹了關(guān)于laravel如何實現(xiàn)按時間日期進行分組統(tǒng)計的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用laravel具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

按日期進行分組

//統(tǒng)計七天內(nèi)注冊用戶數(shù)量按天進行分組
$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();

#獲取的用戶分組數(shù)據(jù)
{
 "date": "2018-01-01", #日期
 "value": 199  #數(shù)量
{
 "date": "2018-01-02",
 "value": 298
},
{
 "date": "2018-01-03",
 "value": 1000
}
 
#在進行圖表統(tǒng)計的時候直接從數(shù)據(jù)庫取得數(shù)據(jù)有些日期可能是沒有的,就需要我們手動進行補全一些日期
#計算日期內(nèi)天數(shù)
$stimestamp = strtotime($start_time);
$etimestamp = strtotime($end_time);
#計算日期段內(nèi)有多少天
$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)計一年內(nèi)注冊用戶數(shù)量按月份進行分組
$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();
#獲取的用戶分組數(shù)據(jù)
{
 "date": "2018-01", #月份
 "value": 1497  #數(shù)量
},
{
 "date": "2018-02",
 "value": 2354
},
{
 "date": "2018-03",
 "value": 4560
} 
#在進行圖表統(tǒng)計的時候直接從數(shù)據(jù)庫取得的數(shù)據(jù)有的月份可能是沒有的,不過月份比較少可直接寫死,同樣也需要補全
$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實現(xiàn)各時間段數(shù)量統(tǒng)計、方便直接使用

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

use Carbon\Carbon;
 
public function getNumber()
{
  $data = [];

  #今天數(shù)據(jù)
  $data['customer_today'] = Customer::where('customer_type', 1)->where('created_at', Carbon::today())->count();
  #昨天數(shù)據(jù)
  $data['customer_yesterday'] = Customer::where('customer_type', 1)->where('created_at', Carbon::yesterday())->count();

  // 本周數(shù)據(jù)
  $this_week = [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()];
  $data['customer_this_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $this_week)->count();

  // 上周數(shù)據(jù)
  $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();

  // 本月數(shù)據(jù)
  $data['customer_this_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->month)->count();

  // 上月數(shù)據(jù)
  $data['customer_last_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count();

  // 本年數(shù)據(jù)
  $data['customer_this_year'] = Customer::where('customer_type', 1)->whereYear('created_at', Carbon::now()->year)->count();

  
  return $data;
}

總結(jié)

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

相關(guān)文章

最新評論

观塘区| 大同市| 台州市| 林甸县| 德庆县| 炎陵县| 孝昌县| 玛多县| 嘉黎县| 长阳| 德昌县| 西藏| 得荣县| 本溪| 大丰市| 城固县| 太仓市| 武城县| 长丰县| 徐水县| 东安县| 鲜城| 越西县| 洞口县| 高淳县| 乌拉特后旗| 会同县| 黄龙县| 吉隆县| 台中市| 塔城市| 天津市| 汽车| 分宜县| 宜春市| 全椒县| 博野县| 安福县| 兴义市| 呼玛县| 佳木斯市|