发布于 4年前
php中如何获取最近六个月每个月的起始时间和结束时间
public function getMonth($m=6) {
$currentTime = time();
$cyear = floor(date("Y",$currentTime));
$cMonth = floor(date("m",$currentTime));
$months = [];
for($i=$m-1;$i>=0;$i--){
$nMonth = $cMonth-$i;
$cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
$nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
$date = $cyear."-".$nMonth."-1";
$firstday = date('Y-m-01 00:00:00', strtotime($date));
$lastday = date('Y-m-t 23:59:59', strtotime($date));
$months[$nMonth] = [
'start' => $firstday,
'end' => $lastday
];
}
return $months;
}