I want to get all the dates between two given specific dates which will be changing dynamically according to the user requirements.
This works fine for me :
$begin = new DateTime( '2018-07-01' );
$end = new DateTime( '2018-08-10' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$dates = [];
foreach($daterange as $date){
array_push($dates,$date->format('Y-m-d'));
}
return $dates;
But this gives me NULL array when I insert dates dynamically. For example,
$begin = new DateTime($request->date1);
$end = new DateTime(today());
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$dates = [];
foreach($daterange as $date){
array_push($dates,$date->format('Y-m-d'));
}
return dates;
I have tried many inputs like storing today's date in a variable, changing it's format and then using that variable as an input. I tried changing date variable to string using (string) function and also I have used Carbon classes for dates, but somehow I am making mistake using DateTime, DateInterval and DatePeriod as I don't know the basics of them.
I need to get all the dates between two specific dates for charts in my admin panel.