Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Localizing Dates and Currencies

Premium
3 min read

Translating all the months and currency names manually yourself would be a lot of work. Luckily, in Laravel we can use PHP packages to help with this!


Localizing Dates

Localizing dates with Carbon is really easy. All you need to do is to set the locale of the Carbon instance to the current locale. This can be done in the AppServiceProvider:

app/Providers/AppServiceProvider.php

use Carbon\Carbon;
 
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// ...
 
Carbon::setLocale(app()->getLocale());
 
//...
}
}

Now, when you use Carbon in your views, it will automatically use the correct locale:

resources/views/welcome.blade.php

{{ now()->isoFormat('dddd, D MMMM YYYY') }}

Which will output:

  • English: Monday, 3 April 2023
  • Spanish: lunes, 3 abril 2023
  • German: Montag, 3 April 2023
  • etc.

It's as easy as that - no need to translate all the months yourself!


Localizing Date Differences

You can also localize the difference between the two dates, in a human-readable format. For example, if you want to show how long ago a post was created, you can use the diffForHumans method:

Controller

$start = now()->subMinutes(56)->subSeconds(33)->subHour();
$end = now();
$difference = $end->longRelativeDiffForHumans($start, 5);
dd($difference);

Which will output...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

V
vinco ✓ Link copied!

when localizing dates with Carbon, how can we change the order and casing of the date format?

Usually, in English it would be Monday, April 3rd 2003 whereas in French for example it would be Lundi 3 avril 2003

M
Modestas ✓ Link copied!

This would require you to have different "bases" of 'dddd, D MMMM YYYY' per each of your locale. These allow you to change the order, structure and so on.