5

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?

bcmcfc
  • 25,966
  • 29
  • 109
  • 181

4 Answers4

6

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
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • More specifically (and also simpler): `\IntlDateFormatter::create('de_DE', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE)->format(\time())` – caw Jun 25 '18 at 16:13
2

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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
0

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'";

?>

Jim
  • 809
  • 1
  • 7
  • 18
0

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.

Paragon
  • 2,692
  • 3
  • 20
  • 27