php - Date format returning Call to a member function format() on string

I have the following which creates an array of dates between 'start' and 'end' dates.
use Carbon\Carbon;
$from = Carbon::createFromFormat('U', $start);
$to = Carbon::createFromFormat('U', $end);
$dates = [];
for ($date = $from; $date->lte($to); $date->addDay()) {
$dates[] = $date->format('d-m-y');
}
I need to format the output of the dates to be in the format 04 October 2020. It is currently returning error 'Call to a member function format() on string' when I try and convert the date.
foreach ($dates as $date) :
echo $date;
endforeach;
I needed the date to be set initially to d-m-y format for this comparision:
foreach ($events as $event) :
if (date('d-m-y', $event->start) === $date) :
.....further processing
endif;
endforeach;
Answer
Solution:
Range ofCarbon
instance is typically handled withCarbonPeriod
:
https://carbon.nesbot.com/docs/#api-period
use Carbon\Carbon;
$dates = Carbon::parse('@' . $start)->daysUntil('@' . $end);
Display:
foreach ($dates as $date) :
echo $date->format('d F Y');
endforeach;
Compare:
foreach ($events as $event) :
if ($event->start->isSameDay($date)) :
.....further processing
endif;
endforeach;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: gd library extension not available with this php installation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.