i1919
i1919

Reputation: 53

Parse string date with carbon (Laravel/Php)

I get the date data as string from Google Analytics like following:

"20190809"

How can I do format with carbon like this:

"2019-08-09"

Upvotes: 1

Views: 609

Answers (1)

matticustard
matticustard

Reputation: 5149

You can use Carbon::createFromFormat() to parse a date from an unusual format to a Carbon instance. Then you can format it however you like.

In your case, the following should work:

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

Upvotes: 5

Related Questions