欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

laravel實(shí)現(xiàn)按月或天或小時(shí)統(tǒng)計(jì)mysql數(shù)據(jù)的方法

 更新時(shí)間:2019年10月09日 16:14:03   作者:php_girl  
今天小編就為大家分享一篇laravel實(shí)現(xiàn)按月或天或小時(shí)統(tǒng)計(jì)mysql數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

在PHP里怎么比較簡單的實(shí)現(xiàn)按時(shí)間(如按月,按天,按小時(shí))來統(tǒng)計(jì)表里的數(shù)據(jù)呢?

如:要實(shí)現(xiàn)獲取下圖曲線圖數(shù)據(jù)(ps:當(dāng)然也可能是柱狀圖等,數(shù)據(jù)都是一樣的),默認(rèn)獲取七天內(nèi)的數(shù)據(jù),點(diǎn)擊今天,7天,15天,30天可任意切換,其中今天是按小時(shí)統(tǒng)計(jì).

不過我的實(shí)現(xiàn)方法有一個小缺點(diǎn),當(dāng)某個小時(shí)內(nèi)是沒有數(shù)據(jù)的,那么該小時(shí)不會出現(xiàn),不過這個應(yīng)該可以通過前端的形式彌補(bǔ)

好了,廢話不多說,上圖上代碼!

1. 控制器內(nèi)容

  /**
   * [getsellerdata 獲取某時(shí)間段內(nèi)商戶結(jié)算查詢數(shù)據(jù)]
   * @param Request $request [description] start:起始時(shí)間 end:結(jié)束時(shí)間 
   * @return [type]      [description]
   */
  public function getsellerqudata(Request $request){
    $data = $this->dataanalysis->getSellerQuData($request->start,$request->end);
    return $data;    
  }

2. 庫文件內(nèi)容

 /**
   * [getSellerQuData 獲取商戶結(jié)算數(shù)據(jù) 曲線]
   * @param [string] $start [起始時(shí)間]2017-08
   * @param [string] $end  [結(jié)束時(shí)間]
   * @return [type]    [description]
   */
  public function getSellerQuData($name,$start,$end){

    //計(jì)算時(shí)間差值,以決定格式化時(shí)間格式
    $diff = strtotime($end)-strtotime($start);

    //分組條件 1天內(nèi)按小時(shí)分組,否則按天/月分組
    //86400/1天 2678400/1月
    if($diff<86400&&$diff>0){
      $sort = '%H';
    }elseif($diff<2678400){
      $sort = '%Y-%m-%d';
    }else{
      $sort = '%Y-%m';
    }
    //把數(shù)據(jù)添加時(shí)間按格式化時(shí)間分組求和,求和分兩種,一種是直接求和,一種是滿足case when條件的數(shù)據(jù)求和
    $query = DB::table('user_withdrawals as w')->select(DB::raw("FROM_UNIXTIME(created_at,'{$sort}') as thedata,sum(case when w.cash_type = 1 then w.money end) as xiabi,sum(case when w.cash_type = 2 then w.money end) as online,sum(w.money) as alls"))->groupBy(DB::raw("FROM_UNIXTIME(created_at,'{$sort}')"));

    //條件篩選 某時(shí)間段內(nèi)
    if( !empty($start) ){
      $query->whereRaw('w.created_at >= ?',strtotime($start));
    }
    if( !empty($end) ){
      $query->whereRaw('w.created_at <= ?',strtotime($end));
    }

    $data = $query->get();

    return $data;
  }

以上這篇laravel實(shí)現(xiàn)按月或天或小時(shí)統(tǒng)計(jì)mysql數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論