PHP Carbon, get all dates between date range?

You can use the range method of the Carbon class in PHP to get all the dates between a specific date range. The range method takes two parameters, the start date and the end date, and returns an array of Carbon instances for each day in the range.

Watch a course Learn object oriented PHP

Here is an example:


$start = Carbon::parse('2022-01-01');
$end = Carbon::parse('2022-01-15');
$dates = Carbon::range($start, $end, '1 day');

foreach ($dates as $date) {
    echo $date->format('Y-m-d') . PHP_EOL;

This will output all the dates between 2022-01-01 and 2022-01-15, one per line. You can also pass second parameter as a CarbonInterval instance in the range method to specify the step, for example: Carbon::range($start, $end, CarbonInterval::week())