Convert String to Carbon Instance

While Developing applications in Laravel, there comes situations where you have date in simple string format. For instance getting raw response from an API. But you want to convert that string into Carbon instance and use the Carbon helpers on it. Well there's a simple solution to it.

While Developing applications in Laravel, there comes situations where you have date in simple string format. For instance getting raw response from an API. But you want to convert that string into Carbon instance and use the Carbon helpers on it. Well there’s a simple solution to it.

Snippet

$date = Carbon\Carbon::parse($rawDate);

well thats it. You’ll now have a Carbon instance & you can format the date as you like using Carbon helper functions. For instance.

$date->diffForHumans();

$date->format('Y');

If you’ve any questions in mind, leave us a comment below. Do follow us on Twitter.