How can PHP functions be utilized to handle and exclude specific dates, such as holidays, in date calculations?

When handling date calculations in PHP, it may be necessary to exclude specific dates such as holidays. One way to achieve this is by creating an array of holiday dates and then checking if a given date falls within this array before performing any calculations. By excluding these dates, you can ensure accurate date calculations without including holidays.

// Array of holiday dates to exclude
$holidays = array(
    '2022-01-01',
    '2022-07-04',
    '2022-12-25'
);

// Function to check if a given date is a holiday
function isHoliday($date, $holidays) {
    return in_array($date, $holidays);
}

// Example date calculation excluding holidays
$date = '2022-01-01';
if (!isHoliday($date, $holidays)) {
    // Perform date calculation here
    echo "Date calculation performed for $date";
} else {
    echo "Date is a holiday and excluded from calculation";
}