where('id',$car_id)->find(); //最终结构 if(empty($car_info['price_json'])){ $car_info['price_json'] = []; }else{ $car_info['price_json'] = json_decode($car_info['price_json'],true); } $all_price = $this->rili_price($car_info['price'],$car_info['price_json']); //dump($all_price); $this->success(1,$all_price); } private function rili_price($default_price,$car_price) { $month_num = 12; //预算3个月,不可以超过12 $toyear = date('Y'); //今天 年份 $tomonth = date('n'); //今天 几月 $today = date('j'); //今天 几号 //今年每月天数 $toyear_month_days = [ 1 => 31, 2 => $toyear%4 == 0 ? 29 : 28, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31, ]; //明年每月天数 $nextyear_month_days = [ 1 => 31, 2 => ($toyear+1)%4 == 0 ? 29 : 28, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31, ]; //今年假期,明年假期 $toyear_holiday = Db::name('car_price_holiday')->where('year',$toyear)->select(); $nextyear_holiday = Db::name('car_price_holiday')->where('year',$toyear+1)->select(); //提前解析,不在循环里解析 foreach($toyear_holiday as $key => $val){ $toyear_holiday[$key]['date_json'] = json_decode($val['date_json'],true); } foreach($nextyear_holiday as $key => $val){ $nextyear_holiday[$key]['date_json'] = json_decode($val['date_json'],true); } //本月剩余日期 $this_month = []; $tomonth_days = $toyear_month_days[$tomonth]; //本月天数 for($i = 1;$i <= $tomonth_days; $i++){ //新的一天 $date = $toyear.'-'.$tomonth.'-'.$i; $newday = []; $newday = ['name'=>$i,'date'=>$date,'val'=>strtotime($date),'hasTips'=>'false','tips'=>'','istoday'=>-1,'price'=>$default_price,'jq_id'=>0]; $newday['date'] = date('Y-m-d',$newday['val']); //日期格式化 $newday['week'] = date('w',$newday['val']); //周几 //追加价格 foreach($car_price as $key => $val){ if($key == $newday['date']){ $newday['price'] = $val; } } //追加假期 foreach($toyear_holiday as $key => $val){ foreach($val['date_json'] as $k => $v){ if($v == $newday['date']){ $newday['hasTips'] = 'true'; $newday['tips'] = $val['title']; $newday['jq_id'] = $val['id']; } } } //关于今天,放在最后优先级高于假期 if($i == $today){ $newday['hasTips'] = 'true'; $newday['tips'] = '今天'; $newday['istoday'] = 1; } if($i > $today){ $newday['istoday'] = 2; } //当本月的第一天不是周天,前追N条数据 if($i == 1 && $newday['week'] != 0){ for($n = 1;$n <= $newday['week'];$n++){ $this_month[] = ['name'=>'']; } } $this_month[] = $newday; } //追加本月 $all_month[] = [ 'monthTxt' => $toyear.'-'.$tomonth, 'list' => $this_month, ]; //dump($all_month);exit; //剩余几个月 for($m = 1;$m <= $month_num; $m++){ $new_month = []; $m_yeah = $tomonth+$m > 12 ? $toyear+1 : $toyear; //年份,大于12进入下一年 $m_month = $tomonth+$m > 12 ? $tomonth+$m-12 : $tomonth+$m; //月份,大于12进入下一年 //dump($m_yeah); //dump($m_month); $m_month_days = $m_yeah == $toyear ? $toyear_month_days[$m_month] : $nextyear_month_days[$m_month];//本月天数 //dump($m_month_days); //循环这个月 for($d = 1;$d <= $m_month_days;$d++){ //新的一天 $date = $m_yeah.'-'.$m_month.'-'.$d; $newday = []; $newday = ['name'=>$d,'date'=>$date,'val'=>strtotime($date),'hasTips'=>'false','tips'=>'','istoday'=>2,'price'=>$default_price,'jq_id'=>0]; $newday['date'] = date('Y-m-d',$newday['val']);//日期格式化 $newday['week'] = date('w',$newday['val']);//周几 //追加价格 foreach($car_price as $key => $val){ if($key == $newday['date']){ $newday['price'] = $val; } } //追加假期 $holiday = $m_yeah == $toyear ? $toyear_holiday : $nextyear_holiday; foreach($holiday as $key => $val){ foreach($val['date_json'] as $k => $v){ if($v == $newday['date']){ $newday['hasTips'] = 'true'; $newday['tips'] = $val['title']; $newday['jq_id'] = $val['id']; } } } //当本月的第一天不是周天,前追N条数据 if($d == 1 && $newday['week'] != 0){ for($n = 1;$n <= $newday['week'];$n++){ $new_month[] = ['name'=>'']; } } $new_month[] = $newday; } //追加未来几个月 $all_month[] = [ 'monthTxt' => $m_yeah.'-'.$m_month, 'list' => $new_month, ]; } //价格日历结果 //dump($all_month); //假期结果 $holiday = array_merge($toyear_holiday,$nextyear_holiday); //dump($holiday);exit; return [ 'rili'=>$all_month, 'holiday' => $holiday, ]; } }