Combine three strings (day, month, year) and calculate next date PHP?


You need to iterate using three for loops and lookup in given day, month and year. If the day, month and year is available, then put them into a variable.

Example

The PHP code is as follows

 Live Demo

<!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>

Output

This will produce the following output

The next date value is =2018-05-25

Updated on: 13-Oct-2020

389 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements