Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use the Carbon library in Laravel to generate date ranges and check if each date falls on a weekday. You can then filter out any dates that fall on weekdays using the filter method.

Here is an example:

use Carbon\Carbon;

// generate date range from January 1st to January 10th
$startDate = Carbon::parse('January 1, 2022');
$endDate = Carbon::parse('January 10, 2022');
$dateRange = collect(Carbon::parse($startDate)->daysUntil($endDate));

// filter out weekdays (Monday to Friday)
$weekdays = $dateRange->filter(function ($date) {
    return $date->isWeekday();
});

// remove weekdays from the date range
$weekends = $dateRange->diff($weekdays);

// output weekend dates
foreach ($weekends as $date) {
    echo $date->format('Y-m-d') . "\n";
}

In this example, we generate a date range from January 1st to January 10th using the daysUntil method. We then use the filter method to check if each date falls on a weekday using the isWeekday method. We then separate the weekdays and weekends by using the diff method. Finally, we output the weekend dates using a loop and the format method.