Search code examples
phplaravelphp-carbon

How to get first day of specific month & year in Laravel?


I want to get the first day of the specific year and month. This is my request data which comes to controller

 "month" => "August"
 "year" => "2020"

I'm trying to get the first day of that specific month and year by doing this way

         Carbon::parse($request->month)->firstOfMonth();

It gives first day of month properly but year is not correct. It always shows year 2021. I want to get year 2020, month august 1st day data.

What is correct way to get the?


Solution

  • You're only passing the month name into the parse() method. How do you expect Carbon to know the year?

    Carbon::createFromFormat('F Y', $request->month.' '.$request->year)->firstOfMonth();