Search code examples
phpbusiness-logic

Calculating future days based on time


I'm trying to create a function that calculates a delivery time based on today's date.

It needs to do the following:

  • if the time is less than 2pm ; add 3 days to today's date
  • if the time is greater add 5 days
  • if the 3/5 days land on a weekend, then the day needs to be Monday

I was wondering what the best approach would be?

Would it be worth putting valid days , i.e. into an array and checking if the date with days added is in it?

I have something like:

$date = time('H'):
If($date < '14') {
$delivery = date('Y-m-d', strtotime('+3 days')
}
else if($date > '14') {
$delivery = date('Y-m-d', strtotime('+5days')
}
return $delivery;

Solution

  • check for hour and add days:

    function returnDelivery(){
        $date = date('G');
        $delivery = 0;
        if($date < 14){
            $delivery = strtotime('+3 days');
        } else {
            $delivery = strtotime('+5 days');
        }
    
        if(date('w', $delivery) == 6 || date('w', $delivery) == 0){
            $delivery = strtotime('next monday');   
        }
        return $delivery;
    }
    
    echo returnDelivery(); // returns timestamp