I have a site where it calculates the delivery dates for orders. so we have days starting fro 1 to 30 So the main conditions are,
- if someone places an order on Saturday or Sunday, the days should start from Monday (working days are Monday to Friday)
- if someone places an order before 18.00 then the days should start from today or else if should start from the next day.. eg: if the order was place on 2020-07-07 before 18.00, then 7th is the starting day or else its the 8th
- if the delivery date falls on Sunday then it should move to Monday
Below is my code, the problem is that it works only on certain days. Most of the time it adds an extra day.
$thedays = array('1','2','3','4','5');
foreach ($thedays as $days) {
if (date('H') > 17) {
$cutoff = 1;
} else {
$cutoff = 0;
}
$extra_days = 0;
//is today a weekend
if(date('l', strtotime(date('Y-m-d'))) == 'Sunday') {
$extra_days = 1;
}
if(date('l', strtotime(date('Y-m-d'))) == 'Saturday') {
$extra_days = 2;
}
$total1= $cutoff+$extra_days+$days;
$date_1 = date('Y-m-d', strtotime("+".$total1." day"));
$date = date('Y-m-d');
$date2 = $date_1;
$period = new DatePeriod(
new DateTime($date),
new DateInterval('P1D'),
new DateTime($date2)
);
$q = 0;
$d= 0;
foreach ($period as $key => $value) {
$d++;
if ($value->format('N') > 5) {
$q++;
}
}
$total_days_with_weekends = ($days+$q+$extra_days);
$date_1 = date('Y-m-d', strtotime("+".$total_days_with_weekends." day"));
if(date('l', strtotime($date_1)) == 'Saturday') {
$date_1 = date('Y-m-d', strtotime("-1 day"));
}
if(date('l', strtotime($date_1)) == 'Sunday') {
$days= $days+1;
$date_1 = date('Y-m-d', strtotime("+".$days." day"));
}
echo $data_1;
}
Can someone suggest a best way to do this?
Thanks