1

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)
Asitis
  • 382
  • 3
  • 17
  • Maybe a [`do-while`](http://php.net/manual/en/control-structures.do.while.php) 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. – Asitis 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
  • https://stackoverflow.com/a/5532070/2943403 and https://stackoverflow.com/a/336175/2943403 – mickmackusa May 16 '18 at 14:14
  • @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 – Asitis May 16 '18 at 14:19

1 Answers1

2

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');
while(in_array($deliveryDate,$allHolidays)){
    $deliveryDate = date('d-m-Y', strtotime("$deliveryDate +1 day"));
}
echo $deliveryDate;

Output:

16-05-2018
18-05-2018
20-05-2018
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 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 – Asitis 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! – Asitis May 29 '18 at 10:53