yii 框架實現按天,月,年,自定義時間段統計數據的方法分析_php實例

來源:腳本之家  責任編輯:小易  

一年是一個自然年,從一月一日到十二月三十一日,你說的是不是你的工齡或者別的什么,假如是工齡的話,你一月五日正式上班,那么到次年的一月四日,就是滿一年www.13333515.buzz防采集請勿采集本網。

本文實例講述了yii 框架實現按天,月,年,自定義時間段統計數據的方法。分享給大家供大家參考,具體如下:

天(day): 格式Y-m-d

月(month):格式Y-m

年(year):格式Y

時間段(range): 格式Y-m-d

首先計算時間

天0-23小時

$rangeTime = range(0, 23);

月:1-月底

// $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);$days = date("t",strtotime($year . '-' . $month));// 生成1-days的天$rangeTime = range(1, $days);

年:1-12月

$rangeTime = range(1, 12);

時間段;開始時間-結束時間

$stimestamp = strtotime($time);$etimestamp = strtotime($time2);// 計算日期段內有多少天$days = ($etimestamp - $stimestamp) / 86400 + 1;// 保存每天日期for($i = 0; $i < $days; $i++){ $newTimeStamp = $stimestamp + (86400 * $i); $rangeTime[] = date('Y-m-d', $newTimeStamp); $labels[] = date('d', $newTimeStamp) . Yii::t('backend', 'day');}

封裝一下

/** * 獲取label和時間段 * type: day, month, year, range * time: 日期; day為具體的天y-m-d, month為具體的月y-m, year為具體的年y * time2 日期, 時間段的第二個時間 */ public function getLabelAndRangeTime($type, $time, $time2) { if(empty($time)) { $time = date('Y-m-d', time()); } $labels = []; $rangeTime = []; if($type == 'day') { // 生成1-24小時 $rangeTime = range(0, 23); foreach ($rangeTime as $key => $val) { $label = $val . Yii::t('backend', 'hour'); $labels[] = $label; } } else if($type == 'month') { $dateArr = explode('-', $time); if(count($dateArr > 1)) { $year = $dateArr[0]; $month = $dateArr[1]; $time = $year; $time2 = $month; // 獲取當前年月的天數 // $days = cal_days_in_month(CAL_GREGORIAN, $month, $year); $days = date("t",strtotime($year . '-' . $month)); // 生成1-days的天 $rangeTime = range(1, $days); foreach ($rangeTime as $key => $val) { $label = $val . Yii::t('backend', 'day'); $labels[] = $label; } } } else if($type == 'year') { // 生成1-12月 $rangeTime = range(1, 12); foreach ($rangeTime as $key => $val) { $label = $val . Yii::t('backend', 'month'); $labels[] = $label; } } else if($type == 'range') { $stimestamp = strtotime($time); $etimestamp = strtotime($time2); // 計算日期段內有多少天 $days = ($etimestamp - $stimestamp) / 86400 + 1; // 保存每天日期 for($i = 0; $i < $days; $i++){ $newTimeStamp = $stimestamp + (86400 * $i); $rangeTime[] = date('Y-m-d', $newTimeStamp); $labels[] = date('d', $newTimeStamp) . Yii::t('backend', 'day'); } } return [ 'type' => $type, 'time' => $time, 'time2' => $time2, 'rangeTime' => $rangeTime, 'labels' => $labels ]; }

然后查詢數據庫

$query = Order::find(); if($type == 'day') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d %H") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y-%m-%d")' => $time]); } else if($type == 'month') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y-%m")' => ($time . '-' . $time2)]); } else if ($type == 'year') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y")' => $time]); } else if ($type == 'range') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['between', 'FROM_UNIXTIME(pay_at,"%Y-%m-%d")', $time, $time2]); } $data = $query->andWhere(['pay_status' => 2])->groupBy('char_time')->all();

按時間排列下

$dataArr = [];foreach ($data as $allKey => $allVal) { $dataArr[$allVal->char_time]['char_time'] = $allVal->char_time; $dataArr[$allVal->char_time]['total_order'] = $allVal->total_order; $dataArr[$allVal->char_time]['total_order_amount'] = bcdiv($allVal->total_order_amount, 100, 2);}

再按時間獲取對應數據

foreach ($rangeTime as $key => $val) { if($type == 'range') { if (array_key_exists($val, $dataArr)) { $charCountDatas[] = $dataArr[$val]['total_order']; $charAmountDatas[] = $dataArr[$val]['total_order_amount']; } else { $charCountDatas[] = 0; $charAmountDatas[] = 0; } } else { $theNow = strlen($val) == 2 ? $val : '0' . $val; if($type == 'day') { $theTime = $time . ' ' . $theNow; } else if($type == 'month') { $theTime = $time . '-' . $time2 . '-' . $theNow; } else if($type == 'year') { $theTime = $time . '-' . $theNow; } if (array_key_exists($theTime, $dataArr)) { $charCountDatas[] = $dataArr[$theTime]['total_order']; $charAmountDatas[] = $dataArr[$theTime]['total_order_amount']; } else { $charCountDatas[] = 0; $charAmountDatas[] = 0; } } }

封裝下

/** * 時間段內支付訂單量及金額 * type 類型: day, month, year * time: 時間, day: 選擇的時間; month: 表示年;year: 表示年; range: 第一個時間 * time2: 時間: day: ''; month: 表示月;year: ''; range: 第二個時間 * rangeTime 時間段 day: 1-24小時; month: 1-30天; year:1-12月,range: time和time2之間的天 */ public function getDayOrderPayChar($type, $time, $time2, $rangeTime) { $query = Order::find(); if($type == 'day') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d %H") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y-%m-%d")' => $time]); } else if($type == 'month') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y-%m")' => ($time . '-' . $time2)]); } else if ($type == 'year') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y")' => $time]); } else if ($type == 'range') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['>=', 'FROM_UNIXTIME(pay_at,"%Y-%m-%d")', $time]) ->andWhere(['<=', 'FROM_UNIXTIME(pay_at,"%Y-%m-%d")', $time2]); } $data = $query->andWhere(['pay_status' => 2])->groupBy('char_time')->all(); $dataArr = []; foreach ($data as $allKey => $allVal) { $dataArr[$allVal->char_time]['char_time'] = $allVal->char_time; $dataArr[$allVal->char_time]['total_order'] = $allVal->total_order; $dataArr[$allVal->char_time]['total_order_amount'] = bcdiv($allVal->total_order_amount, 100, 2); } $charCountDatas = []; $charAmountDatas = []; foreach ($rangeTime as $key => $val) { if($type == 'range') { if (array_key_exists($val, $dataArr)) { $charCountDatas[] = $dataArr[$val]['total_order']; $charAmountDatas[] = $dataArr[$val]['total_order_amount']; } else { $charCountDatas[] = 0; $charAmountDatas[] = 0; } } else { $theNow = strlen($val) == 2 ? $val : '0' . $val; if($type == 'day') { $theTime = $time . ' ' . $theNow; } else if($type == 'month') { $theTime = $time . '-' . $time2 . '-' . $theNow; } else if($type == 'year') { $theTime = $time . '-' . $theNow; } if (array_key_exists($theTime, $dataArr)) { $charCountDatas[] = $dataArr[$theTime]['total_order']; $charAmountDatas[] = $dataArr[$theTime]['total_order_amount']; } else { $charCountDatas[] = 0; $charAmountDatas[] = 0; } } } $res = [ 'count' => [ 'name' => Yii::t('backend', 'hour_order_pay_count_title'), 'color' => '#99CC33', 'charData' => $charCountDatas ], 'amount' => [ 'name' => Yii::t('backend', 'hour_order_pay_amount_title'), 'color' => '#99CC33', 'charData' => $charAmountDatas ] ]; return $res; }

前端

<div class="clearfix dashboard-time-select"> <div class="time-select"> <div class="row"> <div class="col-lg-2 col-md-2 col-sm-2"> <?= Html::dropDownList('day_type', $type, ['day' => Yii::t('backend', 'day'), 'month' => Yii::t('backend', 'month'), 'year' => Yii::t('backend', 'year'), 'range' => Yii::t('backend','range_time')], ['class' => 'type dashboard-time-type']) ?> </div> <div class="col-lg-7 col-md-7 col-sm-7"> <div class="dashboard-time-box"> <div class="dashboard-time-picker dashboard-time-day <?= ($type == 'day') ? '' : 'hide' ;?>"> <?= DateTimePicker::widget([ 'name' => 'time', 'value' => (!empty($time) && $type == 'day') ? $time : '', 'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time'], 'removeButton' => false, 'pluginOptions' => [ 'format' => 'yyyy-mm-dd', 'startView' => 'month', 'minView' => 'month', 'maxView' => 'month', 'autoclose' => true ] ]) ?> </div> <div class="dashboard-time-picker dashboard-time-month <?= ($type == 'month') ? '' : 'hide' ;?>"> <?= DateTimePicker::widget([ 'name' => 'time', 'value' => (!empty($time) && $type == 'month') ? $time : '', 'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time'], 'removeButton' => false, 'pluginOptions' => [ 'format' => 'yyyy-mm', 'startView' => 'year', 'minView' => 'year', 'maxView' => 'year', 'autoclose' => true ] ]) ?> </div> <div class="dashboard-time-picker dashboard-time-year <?= ($type == 'year') ? '' : 'hide' ;?>"> <?= DateTimePicker::widget([ 'name' => 'time', 'value' => (!empty($time) && $type == 'year') ? $time : '', 'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time'], 'removeButton' => false, 'pluginOptions' => [ 'format' => 'yyyy', 'startView' => 'decade', 'minView' => 'decade', 'maxView' => 'decade', 'autoclose' => true ] ]) ?> </div> <div class="dashboard-time-picker dashboard-time-range <?= ($type == 'range') ? '' : 'hide' ;?>"> <div class="row"> <div class="col-lg-6 col-md-6 col-sm-6 range-start"> <?= DateTimePicker::widget([ 'name' => 'time', 'value' => (!empty($time) && $type == 'range') ? $time : '', 'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time time2'], 'removeButton' => false, 'pluginOptions' => [ 'format' => 'yyyy-mm-dd', 'startView' => 'month', 'minView' => 'month', 'maxView' => 'month', 'autoclose' => true ] ]) ?> </div> <div class="col-lg-6 col-md-6 col-sm-6 range-end"> <?= DateTimePicker::widget([ 'name' => 'time2', 'value' => (!empty($time2) && $type == 'range') ? $time2 : '', 'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time time2'], 'removeButton' => false, 'pluginOptions' => [ 'format' => 'yyyy-mm-dd', 'startView' => 'month', 'minView' => 'month', 'maxView' => 'month', 'autoclose' => true ] ]) ?> </div> </div> </div> </div> </div> <div class="col-lg-2 col-md-2 col-sm-2"> <?= Html::button(Yii::t('backend', 'sure'), ['class' => 'btn btn-success btn-dashboard-time', 'data-url' => $url]) ?> </div> </div> </div></div>

確認按鈕

$('.dashboard-time-select .btn-dashboard-time').click(function() { var url = $(this).attr('data-url'); var timeSelect = $(this).parent().parent(); var type = timeSelect.find('.type').val(); var time = ''; var time2 = ''; if(type == 'day') { time = timeSelect.find('.dashboard-time-day input').val(); } else if(type == 'month') { time = timeSelect.find('.dashboard-time-month input').val(); } else if(type == 'year') { time = timeSelect.find('.dashboard-time-year input').val(); } else if(type == 'range') { time = timeSelect.find('.dashboard-time-range .range-start input').val(); time2 = timeSelect.find('.dashboard-time-range .range-end input').val(); } window.location.href = baseBackend + '/' + url + '?type=' + type + '&time=' + time + '&time2=' + time2 }) $('.dashboard-time-select .dashboard-time-type').change(function() { var type = $(this).val(); $('.dashboard-time-select .dashboard-time-picker').addClass('hide'); $('.dashboard-time-select .dashboard-time-' + type).removeClass('hide'); })

更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優秀開發框架總結》、《smarty模板入門基礎教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總

希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。

潤月指的是農歷,先記2113住:回歸5261年的總長度為365.2422日,朔望4102月的長度為29.5306日,十二個朔望月構成農歷1653年,長度為29.5306×12=354.3546日,比回歸年少10.88天即將近11天,每個月少0.91天,近1天。 如果按上述規定制定歷法,就會出現天時與歷法不合、時序錯亂顛倒的怪現象——這就是矛盾。 為了克服這一缺點,我們的祖先在天文觀測的基礎上,找出了“閏月”的辦法,保證農歷年的正月到三月為春季,四月到六月為夏季,七月到九月為秋季,十月到十二月為冬季,也同時保證了農歷歲首在冬末春初。 所以,潤月是指農歷中多出一個月來,而LZ你所說的365天指的公歷。 希望對LZ有所幫助!謝謝!,閏月時農歷年是多了一個月的 而陽歷與農歷無關 1月31到12月31是固定倫的,年年不變。除了四年多一個2月29內容來自www.13333515.buzz請勿采集。


  • 本文相關:
  • yii框架結合charjs實現統計30天數據的方法
  • yii統計不同類型郵箱數量的方法
  • yii中的relations數據關聯查詢及統計功能用法詳解
  • 解析yii數據庫的增刪查改
  • yii2 批量插入、更新數據實例
  • yii2框架數據庫簡單的增刪改查語法小結
  • yii2.0高級框架數據庫增刪改查的一些操作
  • yii框架表單模型使用及以數組形式提交表單數據示例
  • yii實現mysql多數據庫和讀寫分離實例分析
  • yii+mysql鎖表防止并發情況下重復數據的方法
  • yii實現多數據庫主從讀寫分離的方法
  • yii框架結合charjs統計上一年與當前年數據的方法示例
  • 淺談php fopen下載遠程文件的函數
  • thinkphp模板算術運算相關函數用法分析
  • 通過修改配置真正解決php文件上傳大小限制問題(nginx+php)
  • yii2框架dropdownlist下拉菜單用法實例分析
  • laravel等框架模型關聯的可用性淺析
  • symfony核心類概述
  • codeigniter框架基本增刪改查操作示例
  • laravel使用redis實現網站緩存讀取的方法詳解
  • yaf框架封裝的mysql數據庫操作示例
  • laravel 自動生成驗證的實例講解:login / logout
  • 潤月是不是一年中多一個月嗎,為什么日子還是365天呢
  • 現在是1.5日,到下一年幾號算是一年,是按12個月還是365天算?怎么計算一年?
  • 工資一個月按三十天算嗎月薪每個月休息兩天
  • 借款利息是按月算還是按天算?
  • C語言編程序實現如下功能:輸入任意一個日期的年、月、日的值,求出是星期幾并輸出。
  • 網站首頁網頁制作腳本下載服務器操作系統網站運營平面設計媒體動畫電腦基礎硬件教程網絡安全php基礎php技巧php實例php文摘php模板首頁php編程php實例yii框架入門及常用技巧總結php優秀開發框架總結smarty模板入門基礎教程php面向對象程序設計入門教程php字符串(string)用法總結php+mysql數據庫操作入門教程php常見數據庫操作技巧匯總yii框架結合charjs實現統計30天數據的方法yii統計不同類型郵箱數量的方法yii中的relations數據關聯查詢及統計功能用法詳解解析yii數據庫的增刪查改yii2 批量插入、更新數據實例yii2框架數據庫簡單的增刪改查語法小結yii2.0高級框架數據庫增刪改查的一些操作yii框架表單模型使用及以數組形式提交表單數據示例yii實現mysql多數據庫和讀寫分離實例分析yii+mysql鎖表防止并發情況下重復數據的方法yii實現多數據庫主從讀寫分離的方法yii框架結合charjs統計上一年與當前年數據的方法示例淺談php fopen下載遠程文件的函數thinkphp模板算術運算相關函數用法分析通過修改配置真正解決php文件上傳大小限制問題(nginx+php)yii2框架dropdownlist下拉菜單用法實例分析laravel等框架模型關聯的可用性淺析symfony核心類概述codeigniter框架基本增刪改查操作示例laravel使用redis實現網站緩存讀取的方法詳解yaf框架封裝的mysql數據庫操作示例laravel 自動生成驗證的實例講解:login / logoutphp獲取數組長度的方法(有實例)微信公眾平臺實現獲取用戶openid教你如何使用php session使用php生成二維碼的兩種方法(帶php發送get、post請求的6種方法簡php中把stdclass object轉array的微信公眾平臺網頁授權獲取用戶基laravel框架數據庫curd操作、連貫php刪除數組中空值的方法介紹php從數組中刪除元素的四種方法實yii框架連表查詢操作示例dvwa+xampp搭建顯示亂碼的問題及解決方案zend framework教程之autoloading用法詳解php實現保存網站用戶密碼到css文件(通用型zend framework教程之響應對象的封裝zendphp 面向對象程序設計(oop)學習筆記(三yii入門教程之目錄結構、入口文件及路由設php+jquery+css制作頭像登錄窗(仿qq登陸php 取得瑞年與平年的天數的代碼yii框架防止sql注入,xss攻擊與csrf攻擊的
    免責聲明 - 關于我們 - 聯系我們 - 廣告聯系 - 友情鏈接 - 幫助中心 - 頻道導航
    Copyright © 2017 www.13333515.buzz All Rights Reserved
    3排列五开奖结果