原文:https://blog.csdn.net/sqlquan/article/details/87795918
1、封装
/**
* 获取一个日期范围内的日期
* @param int $interval 日期范围
* @param string $type 取值类型,-:获取之前日期;+:获取之后的日期
* @return array
*/
protected function getDateInterval(int $interval,string $type) :array
{
$dateArr = [];
for ($i = $interval - 1; $i >= 0; $i--) {
array_push($dateArr, date('Y-m-d', strtotime("{$type}{$i} day")));
}
if($type=='+')$dateArr=array_reverse($dateArr);
return $dateArr;
}
2、调用
$rs = $this->getDateInterval(14,'-');
print_r($rs);
array:14 [
0 => "2022-07-13"
1 => "2022-07-14"
2 => "2022-07-15"
3 => "2022-07-16"
4 => "2022-07-17"
5 => "2022-07-18"
6 => "2022-07-19"
7 => "2022-07-20"
8 => "2022-07-21"
9 => "2022-07-22"
10 => "2022-07-23"
11 => "2022-07-24"
12 => "2022-07-25"
13 => "2022-07-26"
]
$rs2 = $this->getDateInterval(14,'+');
print_r($rs2);
array:14 [
0 => "2022-07-26"
1 => "2022-07-27"
2 => "2022-07-28"
3 => "2022-07-29"
4 => "2022-07-30"
5 => "2022-07-31"
6 => "2022-08-01"
7 => "2022-08-02"
8 => "2022-08-03"
9 => "2022-08-04"
10 => "2022-08-05"
11 => "2022-08-06"
12 => "2022-08-07"
13 => "2022-08-08"
]
原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/277116.html