I'm building a delivery-date system which is pretty simple in concept; it checks the current date against an array of holiday dates, some other variables and adds 1 day if it needs to, like this:
$currentDate = date('d-m-Y');
$deliveryDate = date('d-m-Y', strtotime($currentDate, '+1 day');
$allHolidays = array('01-05-2018', '02-05-2018', '03-05-2018');
if ($outOfStock) {
$deliveryDate = date('d-m-Y', strtotime(currentDate, '+2 days');
}
[..and so on ]
return $deliveryDate;
So my problem is about the holidays; I want to get the variable $deliveryDate
and check it against the $allHolidays
array. If it exists, I want to 'add +1 day' and check it again.
So for instance if May 1st, 2nd and 3rd are holidays, and the $deliveryDate
would be May 1st before checking, it would see that May 1st is in the $allHolidays
array, then update that '+1 day' to May 2nd, and check it again. Still in the array? Add another day. Until the result would be May 4th.
if (in_array($deliveryDate, $allHolidays)) {
$deliveryDate = date("j-F-Y", strtotime($ddate. "+1 day"));
}
This is the code I have now. It checks 1 time only. How can I update the variable in this check, and have it check again? I have a strong feeling I need to use for
and while
loops, but I haven't a clue how to structure them. I tried wrapping the whole code in a while-loop, but no luck yet.
Edit: Using a do-while loop could work, but how do I get it to recognized the updated variable? If I update the variable it uses in the while-logic in the do, how can it loop properly?
do {
$deliveryDate = date("j-F-Y", strtotime($deliveryDate. "+1 day"));
} while (in_array($deliveryDate, $allHolidays)