结合三个字符串(日、月、年)并计算下一个日期 PHP?
phpserver side programmingprogramming更新于 2025/8/31 1:58:00
您需要使用三个 for 循环进行迭代,并在给定的日、月和年中进行查找。如果日、月和年可用,则将它们放入变量中。
示例
PHP 代码如下
<!DOCTYPE html>
<html>
<body>
<?php
$givenDate = '2018-04-28';
$fiveYears = '2018,2019,2020,2021,2022';
$fiveMonths = '03,05,07,08,09';
$fiveDays = '25,26,27,28,29';
$fYears = explode(',', $fiveYears);
$fMonths = explode(',', $fiveMonths);
$fDays = explode(',', $fiveDays);
$nextDate = null;
foreach($fYears as $yr) {
foreach($fMonths as $mn) {
foreach($fDays as $dy) {
$t = $yr.'-'.$mn.'-'.$dy;
if($t > $givenDate) {
$nextDate = $t;
break 3;
}
}
}
}
if($nextDate) {
echo 'The next date value is =' . $nextDate;
}
else {
echo 'No date is found.';
}
?>
</body>
</html>
输出
这将产生以下输出 −
The next date value is =2018-05-25

