Are there any built-in PHP functions to exclude weekends when calculating date differences?

When calculating date differences in PHP, there are no built-in functions to exclude weekends automatically. To exclude weekends, you can create a custom function that takes into account the start and end dates, then iterate through each day to check if it falls on a weekend (Saturday or Sunday) and exclude those days from the calculation.

function getDateDifferenceWithoutWeekends($start_date, $end_date) {
    $start = new DateTime($start_date);
    $end = new DateTime($end_date);

    $interval = new DateInterval('P1D');
    $period = new DatePeriod($start, $interval, $end);

    $days = 0;
    foreach ($period as $date) {
        if ($date->format('N') < 6) { // Check if the day is not Saturday (6) or Sunday (7)
            $days++;
        }
    }

    return $days;
}

$start_date = '2022-01-01';
$end_date = '2022-01-10';
$days_without_weekends = getDateDifferenceWithoutWeekends($start_date, $end_date);

echo "Number of days between $start_date and $end_date excluding weekends: $days_without_weekends";