Aleks Per
Aleks Per

Reputation: 1639

Laravel Carbon format wrong date

I'm using Laravel and Carbon to format dates.

I have this code:

print_r($data['date']); //Saturday, 11 Jan, 2020
$data['date'] = Carbon::parse($data['date'])->format('Y-m-d');
dd($data['date']);

but the output is:

Saturday, 11 Jan, 2020

"2019-01-12"

As you can see 2020 is converted to 2019... Why? How to fix this problem ?

Upvotes: 2

Views: 2855

Answers (1)

Alex Howansky
Alex Howansky

Reputation: 53563

I'm not sure about Laravel, but the standard DateTime parser does not understand the format Saturday, 11 Jan, 2020. You'll have to explicitly tell the parser how to parse the string. Something like:

$date = DateTime::createFromFormat('l, j M, Y', $data['date'])->format('Y-m-d');

Upvotes: 1

Related Questions