The forming of the date of the number of days(+ condition). Where is the error?

In the variable $DELIVERY is the number of days for delivery.
I need to calculate the exact date when will come order.
But there are conditions:
1) on Saturday and VSK orders not moving
2) Orders are shipped at 12:00 (i.e. after 12:00 PM +1 day)
3) delivery is made only on MON,WED,THU,FRI
4) $DELIVERY specified delivery time to warehouse, to office 1 day.
$DELIVERY=5;
$date_w=date('w');

 if(($date_w==0) || ($date_w==6)){
 if ($date_w==6) $DELIVERY=$DELIVERY+1; 
 } else {
 if ($date_w==5) $DELIVERY=$DELIVERY+1; 
 if (date('H')>12) $DELIVERY=$DELIVERY+1; 
}
$DELIVERY=$DELIVERY+1;
 $DELIVERY=$DELIVERY+abs((intval($DELIVERY / 5)*2)-2);


 if ($DELIVERY==5) $DELIVERY=$DELIVERY+2;
 if(($date_w==2) || ($date_w==4)){
 $DELIVERY =$DELIVERY +1;
}


 if(date("w",time()+86400*$DELIVERY)==6) $DELIVERY=$DELIVERY+2; 
 if(date("w",time()+86400*$DELIVERY)==0) $DELIVERY=$DELIVERY+1; 

 $DELIVERY_DT=date("d.m.y",time()+86400*$DELIVERY);


Sometimes true and sometimes not true believes. Where the error(s?)?
July 8th 19 at 16:01
1 answer
July 8th 19 at 16:03
Solution
<?php

define('C_TIME_ONE_DAY', 86400);

/**
 * @param int $from date of order
 * @param int $daysCount Base the number of days of delivery
 * @param int $nextDayHour Orders made after this time are delivered the next day
 * @param array $freezeDays Days of the week that orders are not moving
 * @param array $deliveryDays Days delivery
 * @return int
*/
function delivery($from, $daysCount, $nextDayHour, $freezeDays, $deliveryDays)
{
 if(!in_array(date('w', $from), $freezeDays)) {
 if(date('H', $from) --> $nextDayHour - 1) {
 $from = strtotime('tomorrow', $from);
 } else {
 $from = strtotime('midnight', $from);
}
}

 while($daysCount > 0) {
 if(!in_array(date('w', $from), $freezeDays)) {
$daysCount--;
}
 $from += C_TIME_ONE_DAY;
}

 while(!in_array(date('w', $from), $deliveryDays)) {
 $from += C_TIME_ONE_DAY;
}

 return $from;
}

/*
 * 5 days for delivery
 * Order made at 12:01 begins to be delivered the next day
 * Saturday and Sunday orders are not moving
 * warehouse gives orders on mon,Wed,Thu,Fri
*/
$deliveryDate = delivery(strtotime('2016-06-06 12:08'), 5, 12, [0,6], [1,3,4,5]);

echo 'warehouse: ', date('Y-m-d', $deliveryDate), PHP_EOL;
echo 'office: ', date('Y-m-d', $deliveryDate + C_TIME_ONE_DAY), PHP_EOL;
m.. thank you) did Not count on ready function.. only
echo 'office: ', date('Y-m-d', $deliveryDate + C_TIME_ONE_DAY), PHP_EOL;
not so , and
echo 'office: ', date('Y-m-d', delivery(strtotime('2016-06-06 12:08'), 5+C_TIME_ONE_DAY, 12, [0,6], [1,3,4,5])), PHP_EOL;

For +1 day may fall on a weekend or a Tuesday. - Viva_Goldner commented on July 8th 19 at 16:06
You are now done shipping length 86405 days ;) - Amanda.Dach commented on July 8th 19 at 16:09
Oh, yeah..) +1 then)) - Viva_Goldner commented on July 8th 19 at 16:12

Find more questions by tags PHP