Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

German Umlauts in strftime date-formatting - correct utf-8 encoding?

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.

like image 614
matt Avatar asked Oct 01 '12 20:10

matt


3 Answers

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");
like image 52
lorem monkey Avatar answered Nov 15 '22 15:11

lorem monkey


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
like image 3
user1460043 Avatar answered Nov 15 '22 16:11

user1460043


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

like image 2
JvdBerg Avatar answered Nov 15 '22 16:11

JvdBerg