Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BilboBago's avatar

Localizing date in view Laravel 5

I'm dealing with date localization in Laravel. Here is how i display date in blade.php.

Publish on {{  $post->published_at->format('F j, Y') }} //Publish on November 20, 2015

I tried with (https://github.com/jenssegers/date) to

use Jenssegers\Date\Date;

Date::setLocale('hr');
echo Date::now()->format('F j, Y'); //Studeni 20, 2015

and what bothers me is how can i reformat $post->publiched_at in blade.php to be localized

0 likes
4 replies
Jorge's avatar
Jorge
Best Answer
Level 19

You have accessors for that: http://laravel.com/docs/5.1/eloquent-mutators#accessors-and-mutators

In your model

use Jenssegers\Date\Date;

class Post extends Model
{
    public function getDates()
    {
        return ['published_at'];
    }

  public function getCreatedAtAttribute($date)
    {
        return new Date($date);
    }

    public function getUpdatedAtAttribute($date)
    {
        return new Date($date);
    }

   public function getPublishedAtAttribute($date)
    {
        return new Date($date);
    }   

}

That way your timestamps will localized to the locale key defined in the config/app.php file

config/app.php

'locale' => 'cr', // your lang
BilboBago's avatar

Well i can't figure why formatLocalize() returns me still english localized date,

<?php Carbon::setLocale('hr'); ?>
 {{$post->published_at->formatLocalized('%A %d %B %Y')}}

but diffForHumans() works right.

<?php Carbon::setLocale('hr'); ?>
 {{$post->published_at->diffForHumans()}}
1 like
cariboufute's avatar

If you use Homestead, it means that you use Linux Ubuntu in your virtual machine. Then, as written here in Carbon documentation, you should check if your system has the locale files installed first.

You can check this in your terminal with this command.

locale -a

You should have a list of locale files like this one

C
C.UTF-8
en_AG
en_AG.utf8
[...]
POSIX

Check if you have a hr_HR file (the Croatian file, right?) in the list. If you don't, you need to install it with this command.

sudo apt-get install language-pack-HR

This will install all locale packages for the languages.

Then, restart your PHP service.

sudo service php5-fpm restart

You can now check if your new locale packages have been read with locale -a

Now, in your view, before using the formatLocalized method, you need to set the PHP locale for time with this command. You can call it in your controller before returning the view.

setlocale(LC_TIME, 'hr_HR.utf8');

You can call it in your controller before returning the view. Please note that you must add .utf8 if you use UTF-8 in your files, to avoid the usual UTF-8 misconversion question marks!

Finally, you can call your formatLocalized method in your Blade template with unescaping braces. If you use the usual braces ({{ }}), nothing will show.

{!! $post->published_at->formatLocalized('%A %d %B %Y') !!}

And there you go!

By the way, the diffForHumans method still works without PHP locale setting because this method doesn't use PHP functions to localize.

2 likes

Please or to participate in this conversation.