Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get PHP to use internationalised dates?

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?

like image 709
bcmcfc Avatar asked Apr 13 '11 16:04

bcmcfc


1 Answers

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
like image 195
Pascal MARTIN Avatar answered Sep 20 '22 10:09

Pascal MARTIN