Reputation: 26745
I'm trying to get PHP dates to work cross language. The language code will be supplied according to the logged in user's language setting.
I thought I could do this:
setlocale(LC_ALL, 'de_DE.UTF-8');
echo strftime('%A %B %Y');
But the output is:
Wednesday April 2011
Whereas I would have expected:
Mittwoch April 2011
(April is the same in English and German)
Is this not the correct way to use the strftime
function? If not, is there an alternative method?
Upvotes: 5
Views: 1664
Reputation: 146370
setlocale() returns a value, which can be FALSE:
Returns the new current locale, or FALSE if the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid.
So you need to check the return value.
Be aware that locale names vary depending on the platform and de_DE.UTF-8
looks like a typical Unix name. Is it a Unix server? If so, make sure that the computer actually has such locale installed.
Upvotes: 2
Reputation: 2722
Your use of strftime() appears to be correct according to the manual. I would question your setLocale() settings.
See here for a question very similar to yours on another forum.
Upvotes: 0
Reputation: 829
It looks like you are missing a bit of code. The answer is found here:
http://php.net/manual/en/function.setlocale.php
/* try different possible locale names for german as of PHP 4.3.0 */ $loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge'); echo "Preferred locale for german on this system is '$loc_de'";
?>
Upvotes: 0
Reputation: 400932
You could use the IntlDateFormatter
class (PHP >= 5.3)
$fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL,IntlDateFormatter::FULL,'America/Los_Angeles',IntlDateFormatter::GREGORIAN );
echo "First Formatted output is ".$fmt->format(0);
$fmt = new IntlDateFormatter( "de-DE" ,IntlDateFormatter::FULL,IntlDateFormatter::FULL,'America/Los_Angeles',IntlDateFormatter::GREGORIAN );
echo "Second Formatted output is ".$fmt->format(0);
Will output :
First Formatted output is Wednesday, December 31, 1969 4:00:00 PM PT
Second Formatted output is Mittwoch, 31. Dezember 1969 16:00 Uhr GMT-08:00
Upvotes: 6