PHP 获取每个月订单总额、订单总数
根据搜索“PHP获取前十二个月订单总数”得到下面的代码
1 $currentTime = time(); 2 $cyear = floor(date("Y",$currentTime)); 3 $cMonth = floor(date("m",$currentTime)); 4 5 for($i=0;$i<6;$i++){ 6 $nMonth = $cMonth-$i; 7 $cyear = $nMonth == 0 ? ($cyear-1) : $cyear; 8 $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth; 9 $date = $cyear."-".$nMonth."-1"; 10 $firstday = date('Y-m-01', strtotime($date)); 11 $lastday = date('Y-m-t', strtotime($date)); 12 13 echo $cyear."年".$nMonth."月"; 14 echo "第一天:".$firstday; 15 echo "最后一天:".$lastday,""; 16 }
然后根据公司需求修改了方法:
我的想法是获取两个月份的相差数,然后用for循环出对应的数据
获取两个日期之间相差多少个月方法:
1 /** 2 * 获取两个日期之间相差多少个月份的方法 3 * */ 4 public static function getMonthNum($date1,$date2, $tags='-' ){ 5 $date1 = explode($tags,$date1); 6 $date2 = explode($tags,$date2); 7 return abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]); 8 9 }
订单按月份统计
1 /** 2 * 订单按月份统计 3 * Enter description here ... 4 */ 5 public function actionOrdermonth() { 6 $created_from = Yii::$app->request->get('created_from'); 7 $created_to = Yii::$app->request->get('created_to'); 8 $trade_type = (int)Yii::$app->request->get('trade_type'); 9 10 11 if (!(empty($created_to))){ 12 $monthSum = ECore::getMonthNum($created_from,$created_to); 13 $created_to = explode("-",$created_to); 14 $cyear = $created_to[0]; 15 $cMonth = $created_to[1]; 16 }else{ 17 $monthSum = 9; 18 $currentTime = time(); 19 $cyear = floor(date("Y",$currentTime)); 20 $cMonth = floor(date("m",$currentTime)); 21 } 22 23 for ($i = 0; $i < $monthSum; $i++) { 24 $nMonth = $cMonth-$i; 25 $cyear = $nMonth == 0 ? ($cyear-1) : $cyear; 26 $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth; 27 $date = $cyear."-".$nMonth."-1"; 28 $firstday = date('Y-m-01', strtotime($date)); 29 $lastday = date('Y-m-t', strtotime($date)); 30 31 $map = ['and', '1=1']; 32 $map[] = ['in', 'order_status', OrderService::ORDER_STATUS_SUCCESS]; 33 if (!empty($trade_type)){ 34 $map[] = ['in', 'trade_type', $trade_type]; 35 } 36 $map[] = ['and', ' created_time >= "' . $firstday . '" and created_time < "' . $lastday . '"'];//查询条件 37 $orders = OrderService::listData([Order::tableName()], $map, [], ['get_one' => true, 'fields' => 'sum(total_fee) as total_fee']); 38 39 if (!$orders['total_fee']) { 40 $orders['total_fee'] = 0; 41 } else { 42 if (empty($orders['total_fee']) || $orders['total_fee'] == NULL) { 43 $orders['total_fee'] = 0; 44 } 45 } 46 47 $quantity = OrderService::listData([Order::tableName()], $map, [], ['get_one' => true, 'fields' => 'sum(total_quantity) as total_quantity']); 48 if (!$quantity['total_quantity']) { 49 $quantity['total_quantity'] = 0; 50 } else { 51 if (empty($quantity['total_quantity']) || $quantity['total_quantity'] == NULL) { 52 $quantity['total_quantity'] = 0; 53 } 54 } 55 56 $data[$i]['month'] = $cyear."-".$nMonth; 57 $data[$i]['total_fee'] = $orders['total_fee']; 58 $data[$i]['total_quantity'] = $quantity['total_quantity']; 59 } 60 61 var_dump($data);die; 62 63 64 $this->ajaxExtjs(array_reverse($data) , count($data), '1'); 65 }
同事用while提示了循环获取两个年月份之间的数:
1 $from = "2014-04"; 2 $to = "2018-08"; 3 4 while($from!=$to){ 5 $from = date("Y-m",strtotime("+1 month",strtotime($from))); 6 echo $from."<br>"; 7 } 8 die('done.');
然后根据同事的提示修改了方法:
1 /** 2 * 订单按月份统计 3 * Enter description here ... 4 */ 5 public function actionOrdermonth() { 6 $created_from = Yii::$app->request->get('created_from'); 7 $created_to = Yii::$app->request->get('created_to'); 8 $trade_type = (int)Yii::$app->request->get('trade_type'); 9 10 if (!(empty($created_from))){ 11 $created_from = $created_from; 12 $created_to = $created_to; 13 }else{ 14 $created_from = date("Y-m", strtotime("-10 month"));; 15 $created_to = date('Y-m', strtotime("-1 month"));; 16 } 17 $monthSum = ECore::getMonthNum($created_from,$created_to); 18 $i = 1; 19 while($i < $monthSum){ 20 $created_from = date("Y-m",strtotime("+1 month",strtotime($created_from))); 21 $BeginDate=date($created_from.'-01', strtotime(date("Y-m-d"))); //获取当前月份第一天 22 $BeginDate .= ' 00:00:01'; 23 $EndDate = date('Y-m-d', strtotime("$BeginDate +1 month -1 day")); //加一个月减去一天 24 $EndDate .= ' 23:59:59'; 25 $map = ['and', '1=1']; 26 $map[] = ['in', 'order_status', OrderService::ORDER_STATUS_SUCCESS]; 27 if (!empty($trade_type)){ 28 $map[] = ['in', 'trade_type', $trade_type]; 29 } 30 $map[] = ['and', ' created_time >= "' . $BeginDate . '" and created_time < "' . $EndDate . '"'];//查询条件 31 $orders = OrderService::listData([Order::tableName()], $map, [], ['get_one' => true, 'fields' => 'sum(total_fee) as total_fee']); 32 33 if (!$orders['total_fee']) { 34 $orders['total_fee'] = 0; 35 } else { 36 if (empty($orders['total_fee']) || $orders['total_fee'] == NULL) { 37 $orders['total_fee'] = 0; 38 } 39 } 40 41 $quantity = OrderService::listData([Order::tableName()], $map, [], ['get_one' => true, 'fields' => 'sum(total_quantity) as total_quantity']); 42 if (!$quantity['total_quantity']) { 43 $quantity['total_quantity'] = 0; 44 } else { 45 if (empty($quantity['total_quantity']) || $quantity['total_quantity'] == NULL) { 46 $quantity['total_quantity'] = 0; 47 } 48 } 49 50 $data[$i]['month'] = $created_from; 51 $data[$i]['total_fee'] = $orders['total_fee']; 52 $data[$i]['total_quantity'] = $quantity['total_quantity']; 53 $i++; 54 } 55 56 $this->ajaxExtjs($data , count($data), '1'); 57 58 59 }
转自https://blog.csdn.net/ROVAST/article/details/50281145
1 public static function getMonthNum($start_date,$end_date,$explode='-',$addOne=false){ 2 //判断两个时间是不是需要调换顺序 3 $start_int = strtotime($start_date); 4 $end_int = strtotime($end_date); 5 if($start_int > $end_int){ 6 $tmp = $start_date; 7 $start_date = $end_date; 8 $end_date = $tmp; 9 } 10 11 12 //结束时间月份+1,如果是13则为新年的一月份 13 $start_arr = explode($explode,$start_date); 14 $start_year = intval($start_arr[0]); 15 $start_month = intval($start_arr[1]); 16 17 18 $end_arr = explode($explode,$end_date); 19 $end_year = intval($end_arr[0]); 20 $end_month = intval($end_arr[1]); 21 22 23 $data = array(); 24 $data[] = $start_date; 25 26 27 $tmp_month = $start_month; 28 $tmp_year = $start_year; 29 30 31 //如果起止不相等,一直循环 32 while (!(($tmp_month == $end_month) && ($tmp_year == $end_year))) { 33 $tmp_month ++; 34 //超过十二月份,到新年的一月份 35 if($tmp_month > 12){ 36 $tmp_month = 1; 37 $tmp_year++; 38 } 39 $data[] = $tmp_year.$explode.str_pad($tmp_month,2,'0',STR_PAD_LEFT); 40 } 41 42 43 if($addOne == true){ 44 $tmp_month ++; 45 //超过十二月份,到新年的一月份 46 if($tmp_month > 12){ 47 $tmp_month = 1; 48 $tmp_year++; 49 } 50 $data[] = $tmp_year.$explode.str_pad($tmp_month,2,'0',STR_PAD_LEFT); 51 } 52 53 54 return $data; 55 }

浙公网安备 33010602011771号