Carbon::setLocale() not working Laravel 5.4 - php

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

Related

PHP 8.1: strftime() is deprecated

When upgrading to PHP 8.1, I got an error regarding "strftime".
How do I correct the code to correctly display the full month name in any language?
$date = strftime("%e %B %Y", strtotime('2010-01-08'))
To my dear and late strftime()... I found a way to adapt with IntlDateFormatter::formatObject and here is the link for the references to the schemas:
https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table
... For those who want to format the date more precisely
// "date_default_timezone_set" may be required by your server
date_default_timezone_set( 'Europe/Paris' );
// make a DateTime object
// the "now" parameter is for get the current date,
// but that work with a date recived from a database
// ex. replace "now" by '2022-04-04 05:05:05'
$dateTimeObj = new DateTime('now', new DateTimeZone('Europe/Paris'));
// format the date according to your preferences
// the 3 params are [ DateTime object, ICU date scheme, string locale ]
$dateFormatted =
IntlDateFormatter::formatObject(
$dateTimeObj,
'eee d MMMM y à HH:mm',
'fr'
);
// test :
echo ucwords($dateFormatted);
// output : Jeu. 7 Avril 2022 à 04:56
I've chosen to use php81_bc/strftime composer package as a replacement.
Here the documentation.
Pay attention that the output could be different from native strftime 'cause php81_bc/strftime uses a different library for locale aware formatting (ICU).
Note that output can be slightly different between libc sprintf and this function as it is using ICU.
You can use the IntlDateFormatter class. The class works independently of the locales settings. With a function like this
function formatLanguage(DateTime $dt,string $format,string $language = 'en') : string {
$curTz = $dt->getTimezone();
if($curTz->getName() === 'Z'){
//INTL don't know Z
$curTz = new DateTimeZone('UTC');
}
$formatPattern = strtr($format,array(
'D' => '{#1}',
'l' => '{#2}',
'M' => '{#3}',
'F' => '{#4}',
));
$strDate = $dt->format($formatPattern);
$regEx = '~\{#\d\}~';
while(preg_match($regEx,$strDate,$match)) {
$IntlFormat = strtr($match[0],array(
'{#1}' => 'E',
'{#2}' => 'EEEE',
'{#3}' => 'MMM',
'{#4}' => 'MMMM',
));
$fmt = datefmt_create( $language ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
$curTz, IntlDateFormatter::GREGORIAN, $IntlFormat);
$replace = $fmt ? datefmt_format( $fmt ,$dt) : "???";
$strDate = str_replace($match[0], $replace, $strDate);
}
return $strDate;
}
you can use format parameters like for datetime.
$dt = date_create('2022-01-31');
echo formatLanguage($dt, 'd F Y','pl'); //31 stycznia 2022
There are extension classes for DateTime that have such functions integrated as methods.
echo dt::create('2022-01-31')->formatL('d F Y','pl');
The strftime is obsolete and DateTime::format() provide a quick replacement and IntlDateFormatter::format() provied a more sophisticated slution.
this links will be help you:
https://github.com/modxcms/revolution/issues/15864
https://github.com/php/php-src/blob/1cf4fb739f7a4fa8404a4c0958f13d04eae519d4/UPGRADING#L379-L381
https://www.php.net/manual/en/datetime.format.php
strftime is deprecated PHP 8.1, You can use date function.
$date = date("%e F Y", strtotime('2010-01-08'))
Hey I have also experienced this issue as well so after some research on PHP's official documentation here what I found!
https://www.php.net/manual/en/function.strftime.php
They are saying that it is depricated and use setlocale() function
this also work same as strftime().
For more information please visit official PHP docs of setlocale() https://www.php.net/manual/en/function.setlocale.php
A quick and simple replacement for the deprecated function strftime can be following.
Instead of using (taking the sample from the question)
$date = strftime("%e %B %Y", strtotime('2010-01-08'))
convert that to:
$date = date('d M Y', strtotime('2010-01-08')

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());

Laravel Carbon set locale doesn't apply

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');

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") );

German Umlauts in strftime date-formatting - correct utf-8 encoding?

I'm creating a german date format like this with PHP 14. März 2012 (which is March 14th 2012).
I'm working with $date[0] that contains a unix timestamp and I convert it like this to a readable german date.
$date_day_month = strftime('%d. %B', $date[0]);
$date_year = strftime('%Y', $date[0]);
echo $date_day_month . $date_year;
However I somehow get a question mark for the Umlaut ä like this
14. M�rz 2012
Why is that and how can I fix this? Thanks in advance.
In my case a simple change of the locale did the trick.
Instead of:
setlocale(LC_TIME, "de_DE");
Use:
setlocale(LC_TIME, "de_DE.UTF-8");
In Windows environment (XAMPP), setlocale(LC_TIME, "de_DE.UTF-8") did not solve the problem for me, so I resorted to using locale "de" and then - as #Chris suggested in his comment to another answer - converted the string manually to utf8:
setlocale(LC_TIME, "de");
$maerzLatin1 = strftime('%B', mktime(9, 0, 0, 3, 1, 2016));
$maerzUtf8 = utf8_encode($maerzLatin1); // this one contains "März" UTF8-encoded
You could try to make your webpage utf-8, put this in your head tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
strftime is depending on the right setting of the locale, so check your setlocale() and make sure that locale exists on the machine that php has running.
Update
I ran this code on my server:
setlocale(LC_ALL, 'de_DE');
$date[0] = mktime( 1, 0, 0, 3, 2, 2012 );
$date_day_month = strftime('%d. %B', $date[0]);
$date_year = strftime('%Y', $date[0]);
echo $date_day_month . $date_year;
And that outputs:
02. März2012

Categories