Last active
November 13, 2022 13:07
-
-
Save marcus-at-localhost/2698a72dfcabab999a86 to your computer and use it in GitHub Desktop.
Delivery date calculation - ship till a certain time, add n days for delivery and detect weekends DateTime Class in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// new date/time object | |
$date = new DateTime('now'); | |
// set order time to a Saturday, 2 pm | |
$date->modify('next sat 14:00'); | |
// orders are taken in till 15 minutes before 2pm | |
// http://php.net/manual/de/datetime.formats.relative.php | |
$order_till = new DateTime('front of 14'); | |
// preserve original date | |
$date_min = clone $date; | |
// use interval | |
//$date_min->add(new DateInterval("P1D")); | |
// orders after 1:45 pm are getting shipped the next day | |
if($date > $order_till) | |
{ | |
$date_min->modify('1 weekdays'); | |
} | |
// if it's on a weekend then it gets shipped on monday | |
if($date_min->format('N') >= 6){ | |
$date_min->modify('next monday'); | |
} | |
// add delivery time | |
$date_min->modify('3 weekdays'); | |
var_dump('Ordered:', $date->format('l jS \of F Y H:i:s')); | |
var_dump('Delivery:', $date_min->format('l jS \of F Y H:i:s')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My take with Yasumi lib to take holidays in account