How to get all dates between two dates in PHP Carbon ?


Few days ago i was working on my laravel web application and i need to retrieve all dates between date range. I had two days and i need to get all date between those two dates. I tried to search on google and finally i found solution using php carbon and it's perfect fit for my laravel app.

So i simply done by write code on like as bellow code, I added my two dates on carbon object and then call one static method so let's see code:

Solution:

public function dateRange()

{

$from = Carbon::createFromDate(2017, 7, 21);

$to = Carbon::createFromDate(2017, 6, 21);

$dates = $this->generateDateRange($to, $from);

print_r($dates);

}

private function generateDateRange(Carbon $start_date, Carbon $end_date)

{

$dates = [];

for($date = $start_date; $date->lte($end_date); $date->addDay()) {

$dates[] = $date->format('Y-m-d');

}

return $dates;

}

i hope you find your best solution and enjoy....


June 27, 2017 | 2736 Viewer | Category : PHP Laravel

Related Posts: