I am using this simple script to test on my host which runs on php version 5.6.
The script gives the output "Wednesday" instead off "Woensdag"(dutch)
Why is the day of the week still in English and not in Dutch?
Is this because the server is not correct configured?
<?php
/* Set locale to Dutch */
date_default_timezone_set("Europe/Amsterdam");
setlocale(LC_ALL, 'nl_NL');
echo date("l"); // output: Wednesday instead off "Woensdag" (dutch)
?>
Unfortunately date is not multilingual. If you want to format a language in another language you need to set locale (as you did in your example) and use strftime
Formatting options for strftime do result in "Woensdag" (or "mittwoch in German, etc):
setlocale(LC_TIME, 'en_EN');
echo strftime('%A', time()); // for a Wednesday will output: Wednesday
setlocale(LC_TIME, 'nl_NL');
echo strftime('%A', time()); // for a Wednesday will output: woensdag
Unless you want al lot of things to be influenced by setlocale you might want to specify that you're doing this for time only (as in the example above). Read on setlocale for more info: http://php.net/manual/en/function.setlocale.php
Keep in mind you need to have the locales available on your machine for this to work. Check with locale -a on linux. setlocale has a return value. If it is false something went wrong and you're most likely missing the specified locale.
Related
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
I have a date field in my table material
id: int;
dat_operation : date;
name : varchar(255);
I would like to know how to translate date format in French
I tried with:
<?php echo date("F j Y",strtotime($var['date_operation']));?>
But i have this result
June 14 2016
First, you'll have to set the "locale information", to specify which language you want to use. Keep in mind, that even though you set that language, it needs to be installed on the server you're running on. It most likely is, but you'll notice if the setlocale has no effect (default is English).
The second thing you'll need to know, is that date() isn't affected by this, you'll have to use strftime() instead, which has a slightly different formatting, which you'll find on the documentation.
An example of using French dates with these two functions:
setlocale(LC_ALL, 'fr_FR');
echo strftime("%B %e %Y", strtotime($var['date_operation']));
Reference and documentation:
http://php.net/manual/en/function.setlocale.php
http://php.net/manual/en/function.strftime.php
The modern and rock-solid approach is the intl (from "Internationalization") extension, which offers e.g. the IntlDateFormatter class:
$date = new DateTime('2016-06-14');
$fmt = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE, 'Europe/Paris', IntlDateFormatter::GREGORIAN);
var_dump($fmt->format($date));
string(12) "14 juin 2016"
If you think it's overkill for your project, you can use the legacy strftime() function but you need to change current locale:
$date = strtotime('2016-06-14');
var_dump(setlocale(LC_TIME, 'fr_FR', 'fr')); // Need to try values until you get true
var_dump(strftime('%B %e %Y', $date));
You need to have French locale data installed. In my experience, this works better on Unix-like systems than on Windows.
I am currently developing a a Website that will be hosted on Microsoft Azure Websites. The client (as well as I) live in Austria and thus we would like to have the Austrian (German) locale for time and date - there is a small difference in the dates as we say "Jänner" instead of "Januar" for "January" and "Januar" sounds somewhat strange to an austrian. The standard snippet for returning a german date would be like so:
function ger_date($date){
date_default_timezone_set("Europe/Vienna");
setlocale (LC_ALL, 'de_DE.utf8','de_DE', 'de_DE#euro', 'german');
$longdate = strftime('%d. %B %Y', strtotime($date));
return $longdate;
}
The answer to this question is acutlly fairly simple. To have austrian or swiss date formats, you can change the locales to the corresponding languages.
function ger_date($date){
date_default_timezone_set("Europe/Vienna");
setlocale (LC_ALL, 'de_AT.utf8','de_AT', 'de_AT#euro', 'german_austria');
$longdate = strftime('%d. %B %Y', strtotime($date));
return $longdate;
}
for austrian
or
function ger_date($date){
date_default_timezone_set("Europe/Vienna");
setlocale (LC_ALL, 'de_CH.utf8','de_CH', 'de_CH#euro', 'german_switzerland');
$longdate = strftime('%d. %B %Y', strtotime($date));
return $longdate;
}
for swiss german. (note that there is not much difference in the regional format between german and switzerland)
But please note, that german_austria outputs Jänner, which sometimes can render as J�nner as the last locale ('german_austria', 'german' or 'german_switzerland') is not an UTF8 Locale and 'german-austria.utf8' is not available on my host.
If your system has no UTF-8 compatible locale installed and you can not install a nother one, you can simply wrap the strftime function into the utf8_encode() function from PHP and everything should work like a charm.
When using the PHP to return the timezone abbreviation with either date('T') or strftime('%Z') the result does not translate to the localized version of these abbreviations.
In french I have been told the following at the correct translations:
EDT == HAE
EST == HNE
I have tried the following code example:
<?php
setlocale(LC_ALL, 'fr_FR');
echo strftime('%Z');
echo date('T');
?>
All attempts produce EST/EDT rather than the translated versions.
I get the same on Mac and Linux.
I inspected the Gettext MO file for French, like this:
/usr/local/bin/msgunfmt /usr/share/locale/fr/LC_MESSAGES/texinfo.mo
This shows all the translations for things like months and days.
So you should get translations if you do:
echo strftime("%A %e %B %Y\n");
// gives "Vendredi 27 juin 2014"
But the translation file does not appear to contain translations for EST, or EDT - or in fact any others I looked for.
So I guess the answer is that these strings are simply not translated in the standard locale packages.
I've run locale -a on my server and can see that it's got Arabic locale settings installed:
ar_AE
ar_AE.iso88596
ar_AE.utf8
However, if I set the locale via:
$locale = array('ar_AE', 'ar_AE.iso88596', 'ar_AE.utf8', 'ar');
setlocale(LC_TIME, $locale);
and output it:
strftime('%A %d %B', $current_date)
The displayed date is in English, not Arabic.
Arabic is the only language this isn't working for: the site I'm working on is in 15 languages and all the others display a translated date.
What's going wrong?
This worked for me with no problems at all.
setlocale(LC_ALL, 'ar_AE.utf8');
If this does not work, then there is another code in your PHP file that interferes with the language.