How do you translate the timezone abbreviation in PHP - php

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.

Related

How can I make PHP:s setlocale() to work correctly on localhost?

I am trying to output the current date with different languages. I have used the following code to echo the date 2022-01-01 in Swedish natural language:
<?php
setlocale(LC_ALL, 'sv_SE.UTF-8');
echo strftime("%A %e %B %Y", mktime(0, 0, 0, 1, 1, 2022)) . "<br>";
?>
When this code is run on my web server it shows the expected output in Swedish:
lördag 1 januari 2022
But when testing on my localhost it displays the date is written in English:
Saturday 1 January 2022
I am running Apache on my localhost. I guess that I need to configure Apache or PHP locally to make this work. But I am not sure how or where. Any suggestions?
I found an answer now. It seems that Microsoft uses other locale strings for this. For Swedish it shall be "sve_SWE" instead of "sv_SE".
Read more: MS Language strings, and MS Country/Region strings
So, for a Windows version of localhost it should be:
setlocale(LC_ALL, 'sve_SWE.UTF-8');

Dates in spanish App Engine Standard PHP 5.5

In my environment (App Engine Standard PHP 5.5) I have a problem with the language of the dates. I need to show them in Spanish, but I can not make it work.
In my local environment it is translated without problems, but in production it shows the dates in English.
In my php.ini I already put this:
date.timezone = "America / Argentina / Cordoba"
extension = "intl.so"
And in the code where I need to show the dates in Spanish (Argentina) I tried this:
setlocale(LC_TIME, 'es_AR.UTF-8', 'es_AR', 'es-AR');
echo strftime("%d de %B", strtotime($event_date));
and in this way also:
setlocale(LC_TIME, 'es_ES.UTF-8', 'es_ES', 'es-ES');
echo strftime("%d de %B", strtotime($event_date));
Am I doing something the wrong way?
Thank you very much

how to set date and time to dutch

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.

Output of date/time in German in PHP

Got a bit of an odd question. After creating a PHP script for a friend who lives in Germany, he has decided he needs the time/date in German format, an example being:
Montag, 16.März 2014
Is there a date/time function that can do this in PHP?
Any help much appreciated, Thanks!
Use strftime() and set the locale to German
Example
<?php
setlocale(LC_ALL, "de_DE.utf8");
echo strftime("%A, %e.%B %Y");
?>
Which would output: Donnerstag, 7.August 2014

How to format dates based on locale?

In a template I display the day and month of a specific date :
<div class="jour"><?php echo date('d',strtotime($content->getCreatedAt())) ?></div>
<div class="mois"><?php echo date('M',strtotime($content->getCreatedAt())) ?></div>
This works fine, problem is the month name is in English. Where do I specify that I want the month names in another locale, French for instance ?
Symfony has a format_date helper among the Date helpers that is i18n-aware. The formats are unfortunately badly documented, see this link for a hint on them.
default_culture only applies for the symfony internationalisation framework, not for native PHP functions. If you want to change this setting project wide, I would do so in config/ProjectConfiguration.class.php, using setlocale, and then use strftime rather than date:
// config/ProjectConfigration.class.php
setlocale(LC_TIME, 'fr_FR');
// *Success.php
<div class="jour"><?php echo strftime('%d',strtotime($content->getCreatedAt())) ?></div>
<div class="mois"><?php echo strftime('%b',strtotime($content->getCreatedAt())) ?></div>
Note that this requires locale settings to be enabled on your machine. To check, do var_dump(setlocale(LC_ALL, 'fr_FR')); If the result is false, you cannot use setlocale to do this and probably need to write the translation code yourself. Furthermore, you will need to have the correct locale installed on your system. To check what locales are installed, do locale -a at the command line.
Sorry for butting in so late in the day, but I would like to add my own thoughts here. The best international date format that I have come up with is "%e %b %Y", e.g. 9 Mar 2012. I find this much more readable than the ISO format "%Y-%m-%d", e.g. 2012-03-09. According to the docs, the %x format should be locale sensitive, but it does not work for me, at least not on the iPhone. This may be because Safari is not passing the locale in the HTML headers, I do not know.
It is sometimes useful to use an array with different possible values to setlocale().
Especially to support different environments (windows, linux, ...)
setlocale(LC_TIME, array('fr', 'fr_FR', 'fr_FR.utf8', 'french', 'french_FRANCE', 'french_FRANCE.utf8'));
echo strftime("%A %d %B", strtotime(date("Y-m-d")));
As the documentation states:
If locale is an array or followed by additional parameters then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.

Categories