Use localized datetime from Carbon in Laravel
1 min read

Use localized datetime from Carbon in Laravel

I've been using this great package laravel-localization for translations. I noticed a minor issue with how datetime is resolved for locales in Laravel. The default format($dateTimeFormat) wasn't kicking it and a look at documentation reveals that if you use Carbon\Carbon 2, there are more helper methods to make this easier. Using isoFormat($dateTimeFormat) takes locale into consideration.

You also may know formatLocalized() method from Carbon 1. This method still works the same in Carbon 2 but you should better use isoFormat() instead. It does mean that you might have change all your existing method calls to a new format but it's a much cleaner, neater solution.

->isoFormat(string $format): string use ISO format rather than PHP-specific format and use inner translations rather than language packages you need to install on every machine where you deploy your application. isoFormat method is compatible with momentjs format method, it means you can use same format strings as you may have used in moment from front-end or node.js. Here are some examples:

Official documentation here: https://carbon.nesbot.com/docs/#api-localization