Laravel Carbon set locale doesn't apply - php

I have a problem to return date in french in my laravel project,
in my model I have the following method :
public function getShowDateAttribute()
{
Carbon::setLocale('fr_FR');
return Carbon::parse($this->conference_date)->format('D d F Y');
}
But the date is still in english,
I've tried also
setLocale(LC_TIME,'fr_FR');
But the date is still in english.
I've also tried to use the php date function and the localizedFormat method of Carbon but always same result : date in english,
would you have any idea of the problem ?
( I checked with locale -a and fr_FR is available on my computer )
Thank you

Translating a carbon date using global localized format
Tested in: Laravel 5.8, Laravel 6, Laravel 8
In config/app.php
'locale' => 'id', // The default is 'en', but this time I want localize them to Indonesian (ID)
Then, to make locale output do something like this:
// WITHOUT LOCALE
Carbon\Carbon::parse('2019-03-01')->format('d F Y'); //Output: "01 March 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 minutes ago"
// WITH LOCALE
Carbon\Carbon::parse('2019-03-01')->translatedFormat('d F Y'); // Output: "01 Maret 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 menit yang lalu"
For more information about converting localize dates you can see on below link
https://carbon.nesbot.com/docs/#api-localization

So, here is the new recommended way to handle internationalization with Carbon.
$date = Carbon::now()->locale('fr_FR');
echo $date->locale(); // fr_FR
echo $date->diffForHumans(); // il y a quelques secondes
echo $date->monthName; // décembre
echo $date->isoFormat('LLLL'); // undi 10 décembre 2018 16:20
For more help go here

Just use fr only while setting locale. Other looks fine
Carbon::setLocale('fr');

Related

Carbon how I can display the day of week in Greek? [duplicate]

Ive read several stackoverflows about set locale. I tested locale -a in the terminal to see if my locale was in there, and it was. The following rule of code is added in the appServiceProvider:
public function boot()
{
Carbon::setLocale($this->app->getLocale());
}
The $this->app->getLocale() returns "nl"
Anyone know why Carbon still shows Sunday instead of Zondag for example?
Translating a carbon date using global localized format
Tested in: Laravel 5.8, Laravel 6, Laravel 8
In config/app.php
'locale' => 'id', // The default is 'en', but this time I want localize them to Indonesian (ID)
Then, to make locale output do something like this:
// WITHOUT LOCALE
Carbon\Carbon::parse('2019-03-01')->format('d F Y'); //Output: "01 March 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 minutes ago"
// WITH LOCALE
Carbon\Carbon::parse('2019-03-01')->translatedFormat('d F Y'); // Output: "01 Maret 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 menit yang lalu"
For more information about converting localize dates you can see on below link
https://carbon.nesbot.com/docs/#api-localization
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
Researching I have found two alternative options:
$date = Carbon::now();
$date->locale('de')->translatedFormat('d F Y');
and:
$date = Carbon::now();
$carbonDateFactory = new CarbonFactory([
'locale' => 'de_DE',
'timezone' => 'Europe/Paris',
]);
$carbonDateFactory->make($date)->isoFormat('d MMMM YYYY');
and the ISO compatible format symbols are here
I solved this by calling setLocale on multiple classes:
$locale = 'nl_NL';
\Carbon\Carbon::setLocale($locale);
\Carbon\CarbonImmutable::setLocale($locale);
\Carbon\CarbonPeriod::setLocale($locale);
\Carbon\CarbonInterval::setLocale($locale);
\Illuminate\Support\Carbon::setLocale($locale);
There's also this ServiceProvider that does the same.
In your AppServiceProvider's register function:
setlocale(LC_TIME, 'nl_NL.utf8');
Carbon::setLocale(config('app.locale'));
Then use translatedFormat() instead of format() or formatLocalized() which is deprecated.
This uses the date() patern which works like format() but translates the string using the current locale.
read more here and here.
try this : setLocale(LC_TIME, app()->getLocale());

How to change date format from US to French date format?

Blade.php
<tr>
<td class="lesDates">
<?php
$date = strtotime($formation['annee_obtention']);
echo date('D / M / Y ', $date);
?>
</td>
</tr>
I would like to show it in French long format.
in laravel you can use Carbon to managing date or time in you application.
check out this https://carbon.nesbot.com/docs/
in carbon,you can format the date using simple code. example in Carbon docs
// Let say Martin from Paris and John from Chicago play chess
$martinDateFactory = new Factory([
'locale' => 'fr_FR',
'timezone' => 'Europe/Paris',
]);
$johnDateFactory = new Factory([
'locale' => 'en_US',
'timezone' => 'America/Chicago',
]);
// Each one will see date in his own language and timezone
// When Martin moves, we display things in French, but we notify John in English:
$gameStart = Carbon::parse('2018-06-15 12:34:00', 'UTC');
$move = Carbon::now('UTC');
$toDisplay = $martinDateFactory->make($gameStart)->isoFormat('lll')."\n".
$martinDateFactory->make($move)->calendar()."\n";
$notificationForJohn = $johnDateFactory->make($gameStart)->isoFormat('lll')."\n".
$johnDateFactory->make($move)->calendar()."\n";
echo $toDisplay;
/*
15 juin 2018 12:34
Aujourd’hui à 12:40
*/
echo $notificationForJohn;
/*
Jun 15, 2018 12:34 PM
Today at 12:40 PM
*/
Use this Repository :
https://github.com/jenssegers/laravel-date
To install this library you can follow these instructions:
https://github.com/jenssegers/laravel-date#installation
Using a library as Laravel-Date you will just need to set the language of the app in the Laravel app config file and use its functions to format the date as you want.
Set the language in /app/config/app.php
'locale' => 'fr',
Examples:
use Jenssegers\Date\Date;
Date::setLocale('nl'); //Change this to your preferred locale
echo Date::now()->format('l j F Y H:i:s'); // zondag 28 april 2013 21:58:16
echo Date::parse('-1 day')->diffForHumans(); // 1 dag gelede
Hindi Example:
Date::setLocale('hi');
echo Date::now()->format('l j F Y H:i:s');
French Example:
Date::setLocale('fr');
echo Date::now()->format('l j F Y H:i:s');

Carbon::setLocale() not working Laravel 5.4

I want to print the current date in Spanish with Carbon with this format: Miércoles 31 de octubre 2018 but I only get Wednesday 31 October 2018.
I already used:
Carbon::setLocale('es');
$fecha = Carbon::now()->format('l j F Y');
and:
Carbon::setLocale(LC_TIME, 'es');
$fecha = Carbon::now()->format('l j F Y');
In config/app.php, I tried with:
Carbon\Carbon::setLocale('es');
I also tried es_ES, es_MX, es_US, es_MX.utf8 but it keeps returning the date in English. I am working on Linux and I already added the locales I need.
Does anyone know how to solve this?
hi use this it will works
setlocale(LC_ALL, "es_ES", 'Spanish_Spain', 'Spanish');
echo iconv('ISO-8859-2', 'UTF-8', strftime("%A, %d de %B ", strtotime(Carbon::now())));
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');
This is an answer given by Eimantas Gabrielius
Can be seen here

Convert French date String to date Object PHP Laravel OctoberCms

I would like to convert a String date to an Object date.
String date :
jeu. 26 avril 2018 10:25
to this object date
{ ["date"]=> string(26) "2018-04-26 10:34:50.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(12) "Africa/Tunis" }
so far i've tried :
Carbon::createFromFormat("Y-m-d H:i:s", 'jeu. 26 avril 2018 10:25')
i've got an exception :
Unexpected data found. Unexpected data found. The separation symbol
could not be found Data missing
u have to change the format:
Carbon::createFromFormat("D. d M Y H:i", 'jeu. 26 avril 2018 10:25')
Note: i assume jeu is day name.
You need to set locale and proper wildcard characters to read proper date format
You can find placeholder here when working with locale : http://php.net/manual/en/function.strftime.php
this code should work
<?php
// use this only if you working on other locale want to restore OLD locale back
// ====================================
$oldLocale = setlocale(LC_ALL, 0);
var_dump(setlocale(LC_ALL, 'fr_FR'));
// ^ this must return 'fr_FR' then only we confirm locale is set
// if return FALSE then install 'fr_FR' locale for PHP on server
// ====================================
// day name will be having .(dot) at end according abbreviation
// lun., mar., mer., jeu., ven., sam., dim.
$timeChunks = strptime('jeu. 26 avril 2018 10:25', '%a %d %b %Y %H:%M');
// ====================================
// use this only if you working on other locale want to restore OLD locale
setlocale(LC_ALL, $oldLocale);
// ====================================
$date = Carbon::create(
($timeChunks['tm_year'] + 1900) , // year
($timeChunks['tm_mon'] + 1), // month
$timeChunks['tm_mday'], // day
$timeChunks['tm_hour'], // hour
$timeChunks['tm_min'], // min
$timeChunks['tm_sec'] // second
);
// tm_year starts from 1900 so we need to add it
// tm_mon is 0 to 11 so add 1
var_dump($date);
if any doubts then please comment

Convert string into time in french - PHP

Basically I wangted to change any string that look like this 012014 into Janvier 2014
in french.
So Itried this and none of them worked!
date_default_timezone_set('Europe/Paris');
setlocale(LC_TIME, "fr_FR");
$pubdate = date('MY', strtotime('012014'));
echo $pubdate;
Instead it displays me May2014, is it that it display me the current month and why is that?And how to display it in french?
Much appreciated!
I suggest you to use the DateTime Class (PHP 5 >= 5.2.0) then use strftime to output the date using a given locale
// Set the locale to French
setlocale(LC_TIME, "fr_FR");
// Create a date object from your format
$date = DateTime::createFromFormat('mY', '042014');
// Format the date according to locale settings
strftime("%B %Y", $date->getTimestamp());
Check the strftime documentation for all available formats
If this still don't work, check the presence of the given locale :
// Returns false if the locale is not available on your system
var_dump( setlocale(LC_TIME, "fr_FR") );

Categories