John
John

Reputation: 319

Carbon date format properly not formatting in Laravel?

my parsing date format is in date/month/year (21/02/2010) i am using carbon to change the format to year-month-date (2010-02-21) but it by default its taking the date as month so it's not converting properly and of course when the date is more than 12 its through an error . Here is the code.

$date = $request->date;
$newdate = Carbon::parse($date)->format('Y-m-d');
return $newdate;

Upvotes: 1

Views: 2187

Answers (2)

Pankaj Maurya
Pankaj Maurya

Reputation: 89

try this code

use Carbon\Carbon;

$date = Carbon::createFromFormat('d/m/Y',$request->date)->format('Y-m-d');

return $date;

Upvotes: 0

Hamelraj
Hamelraj

Reputation: 4826

you have to use like below.

use Carbon\Carbon;

$date = $request->date;
$newdate = Carbon::createFromFormat('d/m/Y',$date)->format('Y-m-d');
return $newdate;

Upvotes: 1

Related Questions