此函数返回两个日期之间的日期数组。
现在它工作得很好,除了一些未知的原因,如果我把它放在 11 月或 3 月作为参数,我会得到一个少一天的数组。其他几个月工作完全正常。我完全一无所知。
function getListofDatesInRange2($fromDate, $toDate)
{
$fromDate = str_replace("-","/", $fromDate);
$toDate = str_replace("-","/", $toDate);
$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
$currentDateStr = date("m-d-Y",$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
}
return $dateMonthYearArr;
}
我重新编码了它,一个while循环解决了我的问题。(虽然我不知道问题是什么)
function getListofDatesInRange2($fromDate, $toDate)
{
$fromDate = str_replace("-","/", $fromDate);
$toDate = str_replace("-","/", $toDate);
$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
array_push($dateMonthYearArr, date('m-d-Y', $fromDateTS));
while($fromDateTS < $toDateTS) {
$fromDateTS += 86400;
array_push($dateMonthYearArr, date('m-d-Y', $fromDateTS));
}
return $dateMonthYearArr;
}