'PHP: minus 2 days excluding weekends and holidays
I'm not good at programming, but the task is to calculate another date from a future given date without taking into account weekends and holidays. For example, we set the date May 3, 2022, which is Tuesday. From this date, I need to subtract 2 days, i.e. May 1, 2022, but this is Sunday, so we have to return to the first working day before May 1, i.e. Friday April 29, 2022.
CMS WordPress. At the moment, my design looks like this:
$setDate = get_field('div_registry'); // ACF field, set May 3, 2022
$сutOff = wp_date('j F Y', strtotime('-2 days', strtotime($setDate)));
echo $сutOff; // returns Sunday, May 1, 2022
For holidays, as I understand it, an array of dates is needed, but I don't know how to apply it)
Solution 1:[1]
For weekends you can do something like this:
$setDate = get_field('div_registry'); // ACF field, set May 3, 2022
$?utOff = wp_date('j F Y', strtotime('-2 days', strtotime($setDate)));
if(strpos($cutOff, 'Sunday') !== false) {
$?utOff = wp_date('j F Y', strtotime('-4 days', strtotime($setDate)));
} else if(strpos($cutOff, 'Saturday') !== false) {
$?utOff = wp_date('j F Y', strtotime('-3 days', strtotime($setDate)));
}
echo $?utOff;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dori Rina |
