I'm trying to write some php that outputs the delivery days for a store.
They deliver goods every Thursday, but to 2 locations and they alternate those locations weekly, so this Thursday they will deliver to Foo and the following Thursday they will deliver to Bar, then the following thursday they will deliver to Foo again and so on....
Ideally I'd like to output:
The next delivery to Foo is: [date] The next delivery to Bar is: [date]
Apologies for my code, I've just started learning about time/datetime stuff and it confuses me!
I've have this which I based of another example:
<?php
$nextThursday= strtotime("next Thursday");
$secondThursday=strtotime("next Thursday",$nextThursday);
echo "The next delivery to Foo is: <strong>" . date("l-jS-F",$nextThursday) . "</strong>, Please order before 7am on " . date("l-jS-F",$nextThursday) . " to ensure delivery.<br>";
echo "The next delivery to Foo is: <strong>" . date("l-jS-F",$secondThursday) . "</strong>, Please order before 7am on " . date("l-jS-F",$secondThursday) . " to ensure delivery.<br>";
?>
But wasn't sure how to get this example working as once the current date gets to 'nextThursday' it will then say the delivery dates around the wrong way. Is there a simple way around this? Do I need to use a number of weeks somehow?
Thanks in advance
CodePudding user response:
You can use the date() function to add a specific number of weeks to a date. The date() function takes a format string as its first argument and a timestamp as its second argument. To add a specific number of weeks to a date, you can use the " X week" format string, where X is the number of weeks you want to add.
<?php
// Calculate the next delivery date for Foo
$nextThursday = strtotime("next Thursday");
$nextFooDeliveryDate = date("l-jS-F", strtotime(" 2 week", $nextThursday));
echo "The next delivery to Foo is: <strong>" . $nextFooDeliveryDate . "</strong>, Please order before 7am on " . $nextFooDeliveryDate . " to ensure delivery.<br>";
// Calculate the next delivery date for Bar
$nextBarDeliveryDate = date("l-jS-F", strtotime(" 1 week", $nextThursday));
echo "The next delivery to Bar is: <strong>" . $nextBarDeliveryDate . "</strong>, Please order before 7am on " . $nextBarDeliveryDate . " to ensure delivery.<br>";
?>
This calculates the next Thursday using the strtotime() function, and then uses the date() function to add one or two weeks to that date to calculate the next delivery date for Foo or Bar. It then outputs the results using the echo statement.
CodePudding user response:
I believe you would like to generate a time schedule for delivery (say next 10 weeks?).
Assuming that you want to print the delivery schedule for the next 10 weeks' thursday and first delivery is go to 'Foo'. In that case we may
- use a loop
- make sure the delivery date is updated to next thursday at every iteration
- use Modulus operator (%) to "switch" from Foo to Bar and so on
<?php
// initalize to today first
$xdate = strtotime("Today");
for ($index=0; $index <10; $index ){
$xdate = strtotime("Next Thursday", $xdate);
echo "The next delivery to ". ($index%2==0 ? 'Foo':'Bar') ." is: <strong>" . date("l-jS-F",$xdate) . "</strong>, Please order before 7am on " . date("l-jS-F",$xdate) . " to ensure delivery.<br><br>";
}
You may see the effect at the following sandbox link: