Linda Vanessa
Linda Vanessa

Reputation: 53

Laravel Carbon, how do I get all the dates between two date ranges?

Explame Code:

$start_date = Carbon::createFromDate(2019, 05, 30);
$end_date   = Carbon::createFromDate(2019, 06, 03);

Desired result:

array:4 [▼
  0 => "2019-05-30"
  1 => "2019-05-31"
  2 => "2019-06-01"
  3 => "2019-06-02"
  4 => "2019-06-03"
]

IDEA:

I saw that the documentation didn't find a way to handle similar requirements. Currently my idea is to use a foreach implementation, but I feel that this is not the best.

Upvotes: 1

Views: 5377

Answers (1)

David Heremans
David Heremans

Reputation: 671

You can indeed use CarbonPeriod for this.

$period = CarbonPeriod::create('2019-05-30', '2019-06-03');
$period->toArray();

This should already return the desired result for you. Check the docs for some nice features and options such as excluding start/end dates if needed.

Upvotes: 4

Related Questions