佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > laravel實(shí)現(xiàn)按時(shí)間日期進(jìn)行分組統(tǒng)計(jì)方法示例

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

熱門標(biāo)簽:銀行業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 服務(wù)器配置 Mysql連接數(shù)設(shè)置 電子圍欄 阿里云 團(tuán)購(gòu)網(wǎng)站 Linux服務(wù)器

按日期進(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ù)有些日期可能是沒有的,就需要我們手動(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ù)有的月份可能是沒有的,不過月份比較少可直接寫死,同樣也需要補(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;
}

總結(jié)

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

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

標(biāo)簽:棗莊 衡水 江蘇 萍鄉(xiāng) 蚌埠 大理 衢州 廣元

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《laravel實(shí)現(xiàn)按時(shí)間日期進(jìn)行分組統(tǒng)計(jì)方法示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    松阳县| 宜君县| 宁晋县| 宣威市| 普格县| 三河市| 吉隆县| 武陟县| 从化市| 和林格尔县| 景谷| 永吉县| 浦城县| 昌宁县| 仙桃市| 深水埗区| 福海县| 贺兰县| 梓潼县| 丹寨县| 禹城市| 静宁县| 临澧县| 青神县| 抚顺县| 铜山县| 旌德县| 桃江县| 九寨沟县| 安宁市| 监利县| 苏州市| 米林县| 铜川市| 榆树市| 萍乡市| 绵阳市| 湘乡市| 建阳市| 云龙县| 台北县|