Carbon: formatLocalized('%d %B %Y') month not localized

Created on 3 Sep 2015  路  14Comments  路  Source: briannesbitt/Carbon

Ive tried to localized date to Indonesian format, but month not localized.

        Carbon::setLocale('id');
        $date = Carbon::createFromTimeStamp(strtotime($comment->created_at));
        $date_only = $date->formatLocalized('%d %B %Y');
        echo $date->diffForHumans(); // 3 hari yang lalu 
        echo $date_only; // 31 August 2015


Anyone know how to corectly localized formatted month? or this is bug?

Most helpful comment

Hello !

I had the same issue.

The probleme come from your system.
If you work on Linux do locale -a to list enabled locales.

Next do sudo locale-gen id_ID.UTF-8 to install your locale and do sudo dpkg-reconfigure locales to publish it.

Reboot your system (vagrant reload) and enjoy

David

[EDIT] Thanks to @mauricehofman

setlocale(LC_TIME, 'nl_NL.utf8');

All 14 comments

For formatLocalized you still need to use the php call setlocale(). Its based on strftime();

http://carbon.nesbot.com/docs/#api-localization

Ive tried to use the function from that sample too but still the Month name not localized

setlocale(LC_TIME, 'Indonesia');
Carbon::setLocale('id');

        $date = Carbon::createFromTimeStamp(strtotime($comment->created_at));
        $date_only = $date->formatLocalized('%d %B %Y');
        echo $date->diffForHumans(); // 3 hari yang lalu 
        echo $date_only; // 31 August 2015

it still gave me same result

Hello !

I had the same issue.

The probleme come from your system.
If you work on Linux do locale -a to list enabled locales.

Next do sudo locale-gen id_ID.UTF-8 to install your locale and do sudo dpkg-reconfigure locales to publish it.

Reboot your system (vagrant reload) and enjoy

David

[EDIT] Thanks to @mauricehofman

setlocale(LC_TIME, 'nl_NL.utf8');

@dlepaux Thanks for your tips

@dlepaux Your solution almost worked. I had to do it like this:

setlocale(LC_TIME, 'nl_NL.utf8');

nice

Thanks @dlepaux :)

@dlepaux system reboot is required for this? Is there another way?

@kovinet It seem to be not possible (no hot-plug)

Hi, formatLocalized uses locales installed on your operating system, so if the locale is not supported by your OS, you need to install it. In this case setlocale will return false.

That's why you really should use the new method isoFormat instead with Carbon 2 as documented:
https://carbon.nesbot.com/docs/#api-localization

You'll get directly 823 locales embedded and many more translation features than with formatLocalized in a really more reliable way (will behave the same on any machine where you deploy your app).

@kylekatarnls thanks, this is much better solution, I don't like to depend on system locales.

Alternatively, you can use this:

setlocale(LC_ALL, 'IND');
$formattedDate = strftime('%d %B %Y, %H:%M', strtotime($item->created_at));

What I had to do on Debian 10:

  1. run dpkg-reconfigure locales
  2. select and install locales package (in my case, I choosed fr_FR.utf8)
  3. At start of script
setlocale(LC_TIME, 'fr_FR.utf8');
\Carbon\Carbon::setLocale('fr');
\Carbon\CarbonInterval::setLocale('fr');

Alternatively, you can use this:

setlocale(LC_ALL, 'IND');
$formattedDate = strftime('%d %B %Y, %H:%M', strtotime($item->created_at));

Thank you, this worked

Was this page helpful?
0 / 5 - 0 ratings