How can a function be created or data be stored in an array when filtering out weekends in PHP date calculations?

To create a function that filters out weekends in PHP date calculations, you can loop through a range of dates, check if each date falls on a weekend (Saturday or Sunday), and store the non-weekend dates in an array. This can be achieved by using the `DateTime` class in PHP to manipulate dates and the `array_push()` function to add valid dates to the array.

function filterWeekends($start_date, $end_date) {
    $dates = [];
    $current_date = new DateTime($start_date);
    $end_date = new DateTime($end_date);

    while ($current_date <= $end_date) {
        if ($current_date->format('N') < 6) { // Check if current date is not a weekend
            $dates[] = $current_date->format('Y-m-d');
        }
        $current_date->modify('+1 day');
    }

    return $dates;
}

// Example usage
$start_date = '2022-01-01';
$end_date = '2022-01-31';
$filtered_dates = filterWeekends($start_date, $end_date);

print_r($filtered_dates);