Laravel 5 Carbon global Locale
So this is my bad, Carbon is actually using the php
setlocale();
the
Carbon::setLocale('fr')
method is only for the
->diffForHumans()
method.
Notice that the php setlocale() reference to the locale stored on your OS
to choose one of the installed one use
locale -a
on your console
secondly, you have to use
->formatLocalized()
method instead of
->format()
method
and lastly all the usefull methods like
->toDateString()
->toFormattedDateString()
->toTimeString()
->toDateTimeString()
->toDayDateTimeString()
are not being localized
and lastly you have to use these parsing letters
http://php.net/manual/en/function.strftime.php
Carbon setLocale not working Laravel
You might want to use setLocale(LC_TIME, $this->app->getLocale())
somewhere at the start of your application.
Then if you wish to have the localized date format with local names use the formatLocalized
function
Carbon::now()->formatLocalized('%d %B %Y');
See http://php.net/manual/en/function.strftime.php for parameter for formatting
Wrong locale for dates in Laravel app in production on Heroku
It simply means Heroku does not have it
locale installed on the OS so LC_TIME
won't work and formatLocalized
neither. Hopefully, Carbon has its own translations with even more formats supported than formatLocalized
(which actually rely on OS locales installed). See ->isoFormat()
in https://carbon.nesbot.com/docs/#api-localization
{{ $user->dateBirth ? Carbon\Carbon::parse($user->dateBirth)->isoFormat('DD/MMM/YYYY') : ''}}
My recommandation is to always use ->isoFormat()
so your code works on any machine with no extra install/setting, and those can be customized so you can change the Carbon translations without impacting the rest.
Laravel : How to localize dates within views with Carbon
You need to use the php function setlocale
before setting the localized format in Carbon.
Unfortunately the base class DateTime does not have any localization support. To begin localization support a formatLocalized($format) method was added. The implementation makes a call to strftime using the current instance timestamp. If you first set the current locale with PHP function setlocale() then the string returned will be formatted in the correct locale.
Examples from the docs:
setlocale(LC_TIME, 'German');
echo $dt->formatLocalized('%A %d %B %Y'); // Mittwoch 21 Mai 1975
setlocale(LC_TIME, '');
echo $dt->formatLocalized('%A %d %B %Y'); // Wednesday 21 May 1975
How to set language for Carbon?
try using PHP function setlocale
also check if your hosting allows and gives you the locales you want.
setlocale(LC_TIME, 'es_ES');
Carbon::setLocale('es');
$archive_current_year = Articles::whereBetween('created_at', [
....
Where to put Carbon::setLocale() in Laravel 5.5?
Go to AppServiceProvider.php
and add it to the boot
method
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
\Carbon\Carbon::setLocale(config('app.locale'));
}
}
Related Topics
How to Set Document Root to Be a Subdirectory Using .Htaccess and Not Vhost
Setlocale(Lc_All, 'En_Gb.Utf8') Not Working on Windows
How to Store Variable Values Over Multiple Page Loads
Unable to Scrape Content from a Website
How to Create a Nested Array Out of an Array in PHP
Implementing "Update If Exists" in Doctrine Orm
Restrict Content Access to Logged in Users with PHP
Error Tolerant HTML/Xml/Sgml Parsing in PHP
Is Auto-Initialization of Multi-Dimensional Hash Array Possible in Ruby, as It Is in PHP
Why Is Turning Magic_Quotes_Gpc on Considered a Bad Practice
PHP Fopen() Error: Failed to Open Stream: Permission Denied
$_Server['Remote_Port'] Not Giving Value
"Transfer-Encoding: Chunked" Header in PHP
How to Fetch Account Data from a Provider with Lightopenid
Strpos() with Multiple Needles
Ftp Upload File to Distant Server with Curl and PHP Uploads a Blank File