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....
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example