PHP日期遍历/年月遍历

日期遍历

function date_range($date1,$date2){
	$timestamp1=strtotime($date1);
	$timestamp2=strtotime($date2);
	$days=($timestamp2-$timestamp1)/86400+1;
	$date=[];
	for($i=0;$i<$days;$i++){
		$date[]=date('Y-m-d',$timestamp1+(86400*$i));
	}
	return $date;
}

年月遍历

function range_year_month($startYear = 2022, $startMonth = 7, $endYear = 2023, $endMonth = 10) {
	$arr = [];
	for ($i = $startYear; $i <= $endYear; $i++) {
		for ($j = (($i == $endYear && $startYear < $endYear) ? 1 : $startMonth); $j <= ($i == $endYear ? $endMonth : 12); $j++) {
			$arr[] = [
				'year' => $i,
				'month' => $j,
				'yearMonth' => $i . '-' . \str_pad($j, 2, '0', \STR_PAD_LEFT),
			];
		}
	}
	return $arr;
}

posted on 2022-09-09 11:43  小馬過河﹎  阅读(153)  评论(0)    收藏  举报

导航