嗨,我有三个日期DropoffDate, atd and utd
。逻辑是CCD_ 2和CCD_ 3
如果DropOffDate = 24-12-2019
,则atd = 27-12-2019 and utd = 02-01-2020
我有一系列假期
Array
(
[0] => 25-12-2019
[1] => 01-01-2020
[2] => 18-04-2019
[3] => 26-12-2019
[4] => 01-08-2021
[5] => 30-11-2021
[6] => 04-01-2021
)
现在的要求是找出DropOffDate和atd之间的所有假期,并将那么多天添加到atd,utd也是如此。如果最后一次atd或utd再次放假,再加一天。
到目前为止,我已经写了这个脚本,但它只获取第一个假期,不寻找下一个假期
$holidayList = Array
(
[0] => 25-12-2019
[1] => 01-01-2020
[2] => 18-04-2019
[3] => 26-12-2019
[4] => 01-08-2021
[5] => 30-11-2021
[6] => 04-01-2021
)
$dropDate = '24-12-2019';
$atd = date("d-m-Y", strtotime("+3 days", $dod));
$utd = date("d-m-Y", strtotime("+9 days", $dod));
$dropDate = strtotime($dropDate);
$atd = strtotime($atd);
$utd = strtotime($utd);
foreach($holidayList as $holiday){
$holiDate = date('d-m-Y' , strtotime($holiday));
$holiDate = strtotime($holiDate);
if (($holiDate >= $dropDate) && ($holiDate <= $atd)){
$atd = date('d-m-Y' , $atd);
$atd = date("d-m-Y", strtotime("+1 days", $atd));
}
if(($holiDate >= $dropDate) && ($holiDate <= $utd)){
$utd = date('d-m-Y' , $utd);
$utd = date("d-m-Y", strtotime("+1 days", $utd));
}
}
echo "<b>AT Date".$atd."<br>";
echo "<b>UT Date".$utd;
ATD和UTD应为29-12-2019和5-1-2020。请帮忙!
您的代码有不必要的从时间戳到字符串的日期转换。如果你把所有东西都清理干净,你就会得到预期的日期。
$dropDate = strtotime('24-12-2019');
$atd = strtotime("+3 days", $dropDate);
$utd = strtotime("+9 days", $dropDate);
foreach($holidayList as $holiday){
$holiDate = strtotime($holiday);
if (($holiDate >= $dropDate) && ($holiDate <= $atd)){
$atd = strtotime("+1 days", $atd);
}
if(($holiDate >= $dropDate) && ($holiDate <= $utd)){
$utd = strtotime("+1 days", $utd);
}
}
echo "AT Date ", date('d-m-Y',$atd), "n";
echo "UT Date ", date('d-m-Y',$utd);
// Output:
// AT Date 29-12-2019
// UT Date 05-01-2020
小提琴