Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

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 CarbonCarbon;
$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;
question from:https://stackoverflow.com/questions/65850199/date-format-returning-call-to-a-member-function-format-on-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Range of Carbon instance is typically handled with CarbonPeriod: https://carbon.nesbot.com/docs/#api-period

use CarbonCarbon;

$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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...