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?
You could use the IntlDateFormatter
class (PHP >= 5.3)
Quoting the example given on the manual page of IntlDateFormatter::format()
:
$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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With