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)
  • Maybe a do-while might be what you want? – Script47 May 16 '18 at 14:09
  • @Script47 ; I think it would, but I can't figure out what the 'While' statement would be. – Alex Timmer May 16 '18 at 14:12
  • There are pre-existing StackOverflow pages on this topic of skipping holidays when incrementing dates. Have you had a good solid look around? – mickmackusa May 16 '18 at 14:13
  • @mickmackusa I did see those exact topics before I started working on this. Hadn't checked them for this specific issue tho. Thanks for the input, will go through them again – Alex Timmer May 16 '18 at 14:19

I've put together a demonstration for you.

I've added a little "trick" to use the $outOfStock boolean value to boost the day count appropriately. By adding it to 1, the boolean value is type converted to an int value. true becomes 1 and false becomes 0.

The rest should be pretty self-explanatory... initialize the $deliveryDate, bump it 1 or 2 days, then bump it until it no longer falls on a listed holiday.

Code: (Demo)

$outOfStock = true;
echo date('d-m-Y') , "\n";  // as baseline

$deliveryDate = date('d-m-Y', strtotime('+' . 1+$outOfStock . ' day'));  // treat bool as int: instock=1, outofstock=2
echo $deliveryDate , "\n";  // as proof

$allHolidays = array('17-05-2018', '18-05-2018', '19-05-2018');
    $deliveryDate = date('d-m-Y', strtotime("$deliveryDate +1 day"));
echo $deliveryDate;


  • Thanks man! Your demo shows exactly what I want to achieve. Why my same-y code with the while-loop doesn't work, I think it's in the details of the markup. I will do a closer examination of the code tomorrow, see what I can learn. In any case, you win the points for providing a working answer! :D – Alex Timmer May 16 '18 at 15:15
  • At first glance, it may be the j-F-Y. Comparing apples to oranges. Or strtotime() was misinterpreting the j-F-Y. – mickmackusa May 16 '18 at 15:18
  • Forgot to comment; the crux was in the strtotime syntax; I tried to combine the variable with a dot like $delDate . "+2 days" but it should be all in quotes like "$delDate + 2 days". Thanks again mate! – Alex Timmer May 29 '18 at 10:53

Your Answer

