PHP date() in foreign languages - e.g. Mar 25 Aoû 09 [duplicate]
I have a script that needs to display date data to an international audience - e.g.
"submitted Tue 25 Aug 09"
Is there an easier/cleaner way to get this converted to the French(etc) equivalent "Mar 25 Aoû 09" than:
Setting a constant LANG
and a $LANGUAGES
array of include files & :
if(LANG != 'EN')
{
include $LANGUAGES['LANG'];
}
& then the included file maps the days & months & replaces for the appropriate locale?
I think you can't get away from doing so without setting LOCALE:
<?php
setlocale(LC_ALL, 'fr_FR');
echo strftime("%A %e %B %Y");
?>
Some details on strftime: http://us2.php.net/manual/en/function.strftime.php
According to the date
function's manual page, you should use setlocale
. Methods such as strftime
will then use the locale specified. date
, however, will not for some reason.
I think that the best way to do it with strftime
and setlocale
functions. But it will not work if your server has no needed locale installed (in current questions it is fr_FR
).
Code bellow throw an exception if locale change will be unsuccessful
<?php
$result = setlocale(LC_ALL, 'fr_FR');
if($result === false){
throw new \RuntimeException(
'Got error changing locale, check if locale is installed on the system'
);
}
$dayOfMonth = '%e';
//if it is Windows we will use %#d as %e is not supported
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$dayOfMonth = '%#d';
}
//Mar 25 Aoû 09 - month shortname, day of month, day shortname, year last two digits
echo strftime("%b $dayOfMonth %a %y");