Skip to content

Instantly share code, notes, and snippets.

@rogercbe
Last active June 4, 2018 13:48
Show Gist options
  • Select an option

  • Save rogercbe/2d02a747c9ccf0218b32f78b1eacff8e to your computer and use it in GitHub Desktop.

Select an option

Save rogercbe/2d02a747c9ccf0218b32f78b1eacff8e to your computer and use it in GitHub Desktop.
Laravel macro to extend Carbon and generate array of dates in a daterange

Code

public function boot()
{
    Carbon::macro('range', function ($start, $end) {
        return new Collection(
            new DatePeriod(
                $start, 
                new DateInterval('P1D'), 
                $end->copy()->add(new DateInterval('P1D'))
            )
        );
    });
}

Usage

return Carbon::range($this->startDate, $this->endDate)->map(function($date) {
    return $date;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment