這篇文章將為大家詳細(xì)講解有關(guān)使用laravel怎么實(shí)現(xiàn)按時(shí)間日期分組統(tǒng)計(jì),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
按日期進(jìn)行分組
//統(tǒng)計(jì)七天內(nèi)注冊(cè)用戶數(shù)量按天進(jìn)行分組 $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 } #在進(jìn)行圖表統(tǒng)計(jì)的時(shí)候直接從數(shù)據(jù)庫(kù)取得數(shù)據(jù)有些日期可能是沒(méi)有的,就需要我們手動(dòng)進(jìn)行補(bǔ)全一些日期 #計(jì)算日期內(nèi)天數(shù) $stimestamp = strtotime($start_time); $etimestamp = strtotime($end_time); #計(jì)算日期段內(nèi)有多少天 $days = ($etimestamp - $stimestamp) / 86400; #保存每天日期 $date = array(); for($i = 0;$i < $days;$i++){ $date[] = date('Y-m-d', $stimestamp + (86400 * $i)); } #循環(huán)補(bǔ)全日期 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;
按月份進(jìn)行分組
#統(tǒng)計(jì)一年內(nèi)注冊(cè)用戶數(shù)量按月份進(jìn)行分組 $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 } #在進(jìn)行圖表統(tǒng)計(jì)的時(shí)候直接從數(shù)據(jù)庫(kù)取得的數(shù)據(jù)有的月份可能是沒(méi)有的,不過(guò)月份比較少可直接寫死,同樣也需要補(bǔ)全 $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)補(bǔ)全月份 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實(shí)現(xiàn)各時(shí)間段數(shù)量統(tǒng)計(jì)、方便直接使用
因項(xiàng)目中用到了圖表之類的信息,需要獲取到很多時(shí)間的數(shù)據(jù)動(dòng)態(tài),剛開始我都是自己換算時(shí)間來(lái)計(jì)算,后來(lái) 看到手冊(cè)中有更簡(jiǎn)單的方法,自己總結(jié)了一下通用的時(shí)間段統(tǒng)計(jì)(今天、昨天、上周、本周、上月、本月、上年、本年)。
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; }
關(guān)于使用laravel怎么實(shí)現(xiàn)按時(shí)間日期分組統(tǒng)計(jì)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章題目:使用laravel怎么實(shí)現(xiàn)按時(shí)間日期分組統(tǒng)計(jì)-創(chuàng)新互聯(lián)
瀏覽地址:http://chinadenli.net/article32/dgjpsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站策劃、定制開發(fā)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容