German Umlauts in strftime date-formatting - corre

2020-08-10 07:12发布

问题:

I'm creating a german date format like this with PHP 14. März 2012 (which is March 14th 2012).

I'm working with $date[0] that contains a unix timestamp and I convert it like this to a readable german date.

$date_day_month = strftime('%d. %B', $date[0]);
$date_year = strftime('%Y', $date[0]);

echo $date_day_month . $date_year;

However I somehow get a question mark for the Umlaut ä like this

14. M�rz 2012

Why is that and how can I fix this? Thanks in advance.

回答1:

You could try to make your webpage utf-8, put this in your head tag:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

strftime is depending on the right setting of the locale, so check your setlocale() and make sure that locale exists on the machine that php has running.

Update

I ran this code on my server:

setlocale(LC_ALL, 'de_DE');
$date[0] = mktime( 1, 0, 0, 3, 2, 2012 );

$date_day_month = strftime('%d. %B', $date[0]);
$date_year = strftime('%Y', $date[0]);

echo $date_day_month . $date_year;

And that outputs:

02. März2012



回答2:

In my case a simple change of the locale did the trick.

Instead of:

setlocale(LC_TIME, "de_DE");

Use:

setlocale(LC_TIME, "de_DE.UTF-8");


回答3:

In Windows environment (XAMPP), setlocale(LC_TIME, "de_DE.UTF-8") did not solve the problem for me, so I resorted to using locale "de" and then - as @Chris suggested in his comment to another answer - converted the string manually to utf8:

setlocale(LC_TIME, "de");
$maerzLatin1 = strftime('%B', mktime(9, 0, 0, 3, 1, 2016));
$maerzUtf8 = utf8_encode($maerzLatin1);  // this one contains "März" UTF8-encoded