Change date format Laravel - php

I'm from Indonesia, and I want to show date with Indonesian language format but what I've got was English language format.
Example format was 23 march 2017 (english)
I want to get like this one 23 Maret 2017 (Indonesia) so the problem of mine is language.
Can you give me some idea ? this is my example code
<?php echo date("d F Y", strtotime($row->tanggal_pelaksanaan)); ?>
and
Tgl: #foreach ($tindak as $tindakan)
{{ date("d F Y", strtotime($tindakan->target_verifikasi)) }}
#endforeach
Please Kindly help me. thank you.

See if the locale is installed in your server.
You can list all the locale in your server by issuing the command
locale -a
Install your locale. Indonesian locale, I believe is id_ID by issuing the command. The list of locale is obtained from this answer https://stackoverflow.com/a/28357857/5808894
sudo locale-gen id_ID
sudo update-locale
Set locale using the method setlocale
setlocale(LC_TIME, 'id_ID');
Either use Carbon or strftime to output the localized date
strftime("%a %d %b %Y", strtotime(date('Y-m-d')))
OR
Carbon\Carbon::parse('23-03-2017 11:23:45')->formatLocalized('%A %d %B %Y');
outputs Kamis 23 Maret 2017

$dateName = Carbon::now()->locale('id')->isoFormat('LL'); //return 17 Mei 2020
$dateName = Carbon::now()->locale('id')->isoFormat('LLLL'); //return Minggu, 17 Mei 2020 pukul 10.21
Try this code
and you can check on https://carbon.nesbot.com/docs/#api-localization

Laravel Eloquent allows you format from the model so you can do
$model->created_at->format('Y-m-d')
To do this you should add
$protected $dates = [
'created_at',
'updated_at',
// add other column names here that you want as Carbon Date
];
This will then mutate the columns to be a easily manipulated date object
see https://laravel.com/docs/5.4/eloquent-mutators#date-mutators for further details

You cannot depends on server locale, because most of application was running on their own devices with their own locale format. You can installing the locale format for your own device but not for others because they have their locale timezone.
Use another method like:
{{ date('d-m-y', strtotime($example->start_date))}}
The code above will change
yyyy-mm-dd
format to
d-m-y
2019-05-01 to 01-05-19
Or
{{ date('l, d-m-y', strtotime($example->start_date))}}
Ouput:
Friday, 01-05-2019
Hope it helps.

Related

date format - Regional settings - Months in lowercase

I'm a noob (I confess it) and can't manage to find a solution to my problem.
I'm setting up the date format on Drupal and it uses the PHP date format.
Right now it's "d F Y", so it appears as 07 Dicembre 2021 (in Italian), but in Italian months are written out in lowercase. Is there a way to transform Dicembre into dicembre? I couldn't find a proper way.
Thanks for your help!
Using strtolower() the output will be as you expected:
<?php echo strtolower(date("M"),2);?>
So to implement it only for Month, simply break the date format into 3 parts. Day , Month, Year.
Use drupal module:
https://www.drupal.org/project/simple_field_formatter
Go to:
/admin/structure/types/manage/[YOURTYPE]/display
For your date field, click the gearwheel on the right (format settings) and activate the checkbox for strtolower
Or create your own FieldFormatter: https://www.webwash.net/how-to-create-a-custom-field-formatter-in-drupal-8/
You should use the locale aware strftime, which formats according to the current locale.
setlocale(LC_TIME, "it_IT");
echo strftime("%d %B %Y"); // 07 dicembre 2021

Carbon date language not change on format [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());

Convert English date in French language

I get a $date from an API in UTC format like 2021-11-13T14:00:14Z.
So using php I convert it to : 13 November 2021
$date = strftime(date('d F Y', strtotime($date);
So now I want to translate it in French.
I am working on local with on macbook pro using MAMP.
My html code setting is :
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I even forced Local Time to :
setlocale(LC_TIME, 'fr', 'fr_FR', 'fr_FR#euro', 'fr_FR.utf8', 'fr-FR', 'fra'); date_default_timezone_set('Europe/Paris');
But the date remain in english !
Thanks in advance for your help
You're not calling strftime() correctly. The first argument must be a format string. date() returns an already-formatted date, and it doesn't use locales. This works for me:
setlocale(LC_TIME, 'fr', 'fr_FR', 'fr_FR#euro', 'fr_FR.utf8', 'fr-FR', 'fra');
date_default_timezone_set('Europe/Paris');
$date = "2021-11-13T14:00:14Z";
echo strftime('%d %B %Y', strtotime($date));
Solutions based on the IntlDateFormatter class work independently of the local installations of the server. Example as already written in the comment here. When using the class dt, which uses the IntlDateFormatter class, the same format characters can be used for the method formatL as for DateTime::format.
$dt = dt::create('2021-11-13T14:00:14Z');
//UTC -> localTime "2021-11-13 15:00:14"
$dt->setTimeZone('Europe/Paris');
echo $dt->formatL('l d F Y H:i','fr');
// samedi 13 novembre 2021 15:00
echo $dt->formatL('l d F Y H:i','de');
// Samstag 13 November 2021 15:00
The date string contains the "Z" time zone, which corresponds to UTC.
A new time zone can be set with setTimeZone() to get the local time. If the UTC time (here 14:00) is to be displayed, this line must be omitted. The format method (without L) still exists. This method delivers the result as usual in English.

PHP strftime outputs wrong format despite correct timezone

I configured my timezone to Europe/Paris in php.ini. When executing date_default_timezone_get() I do get the correct value.
Then, I expect strftime('%x', date()) to output something like 16 novembre 2018 which is the French format. But instead, I get 11/16/2018 which looks like the US format.
Any idea why?
The time zone has no effect on how dates and times are presented, for that you need to set the locale. There are no standards for locale names, but fortunately PHP's setlocale() function will take multiple locale names, stopping at the first successful one.
// just a few common name formats
setlocale(LC_TIME, ["fr_FR.utf8", "fr_FR#euro", "fr_UTF8", "fr_FR", "french"]);
echo strftime("%d %B %Y", time());
I tried:
setlocale(LC_ALL, 'fr_utf8');
echo strftime("%d %B %Y", time());
and got:
16 novembre 2018
after setting the timezone to Europe/Paris, Use echo date('d-M-Y') and it will display your desired time. it worked for me

Carbon setLocale not working Laravel

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

Categories