Question

I have the current date, in the format I need: Y/m/d

And I need to calculate what date will be in X days excluding weekends and holidays. X - because this value is dynamic and can be set in the admin panel (For example: X = +2 days).

Can you help with your ideas or examples, how can this be implemented?

How calculate the date excluding weekends and holidays, and count only business days?

Thanks.

Was it helpful?

Solution

I believe you can exclude weekends dynamically but to exclude holidays is tough to handle. Because every country has their own holidays. Not all countries are celebrating same holidays. So you need to configure/define holidays statically and can exclude weekends as below:

 $datetime1 = new DateTime('2020-01-01');
 $datetime2 = new DateTime('2020-01-31');
 $interval = $datetime1->diff($datetime2);
 $woweekends = 0;
 for($i=0; $i<=$interval->d; $i++){
     $modif = $datetime1->modify('+1 day');
     $weekday = $datetime1->format('w');

     if($weekday != 0 && $weekday != 6){ // 0 for Sunday and 6 for Saturday
        $woweekends++;  
     }

  }        
  echo $woweekends." days without weekend";

Output : 23 days without weekend

Hope this help to you.

Thanks,

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top