PHP 获取当前时间前52周 12个月 4个季度
private function getWeeks($now,$weeks=52){ $weeksList = []; for($i=1;$i<=$weeks;$i++){ $week = (int)date("w",$now); //当时是周几 $number = $week == 0 ? 7 : $week; //如遇周末,将0换成7 $diffDay = $number - 1; //求到周一差几天 $endDiffDay = 7 - $number; //到周末还有几天 $startDate = date("Y-m-d",$now - ($diffDay * 86400)); $endDate = date("Y-m-d",$now + ($endDiffDay * 86400)); $weeksList[$i] = [$startDate,$endDate]; $now = strtotime($startDate)-1; } return $weeksList; }
周:
private function getQuarter($y,$m,$month31,$quarterTimes=4){ $quarterList = []; $year = $y; for($i=1;$i<=$quarterTimes;$i++){ # 每个季度月份 $quarter = [ ['01','02','03'], ['04','05','06'], ['07','08','09'], [10,11,12] ]; foreach ($quarter as $v){ if(in_array($m,$v)){ $startDate = date($y.'-'.$v[0].'-01'); if(in_array((int)$v[2],$month31)){ $endDate = date($y.'-'.$v[2].'-31'); } else{ $endDate = date($y.'-'.$v[2].'-30'); } # 不存在 2月份结束的 季度 } } $quarterList[$i] = [$startDate,$endDate]; if($m==1 && $y != $year){ $m=12; $y = $year; } else { $m = date('m',strtotime($startDate)) - 3; } if($m == 1){ $year-=1; continue; } } return $quarterList; }
季度:
private function getMonths($y,$m,$month31,$month=12){ $monthList = []; $year = $y; for($i=1;$i<=$month;$i++){ if($m<10){ $m = '0'.$m; } $startDate = date($y.'-'.$m.'-01',time()); if(in_array($m,$month31)){ $endDate = date($y.'-'.$m.'-31',time()); } elseif($m != 2){ $endDate = date($y.'-'.$m.'-30',time()); } else { if ($y%4==0 && ($y%100!=0 || $y%400==0) && $m == 2){ $endDate = date($y.'-'.$m.'-29',time()); }else{ $endDate = date($y.'-'.$m.'-28',time()); } } $monthList[$i] = [$startDate,$endDate]; if($m==1 && $y != $year){ $m=12; $y = $year; } else { $m = date('m',strtotime($startDate)) - 1; } if($m == 1){ $year-=1; continue; } } return $monthList; }
月: