2

This is about wrong display of spelled-out German month names which contain special characters (Umlaut, basically only the letter "ä" in "März" - German for March). I am trying to display it like "28. März 2012".

This is in Wordpress, BTW. In the header I have the <meta charset="utf-8"> tag. The corresponding template file contains the following line to get the month names in German:

setlocale (LC_ALL, 'de_AT@euro', 'de_AT', 'de', 'ge');

Actually, this code is for "Austrian German", but it shouldn't make a difference for the month of March = März.

For the page I am fetching a string from the database which represents a date, for example "18.03.2012". Then I convert that string to a "real" PHP date():

$date_start = date_create($date_string_from_database);

Since I need to compare that date to another timestamp, I convert it to a timestamp like this (adding a fixed time of 20:00):

$timestamp_start = strtotime(date_format($date_start, 'Y-m-j 20:00'));

And sometime later I create a formated date with spelled-out German month names from this timestamp like this:

$date_german = strftime("%e. %B %Y", $timestamp_start);

Now, in my localhost installation, this is displayed as expected:

enter image description here

But on the performance server, I get this:

enter image description here

All other special characters (including "ä") on that page are displayed correctly, it must have to do with the way the server handles that strftime function or the setlocale settings. FWIW, also the generated HTML code contains that "?" character instead of "ä". The PHP version is 5.6 in both cases.

Any idea what I could do to get it displayed correctly?

EDIT / ADDITION concerning possible duplicate: This has nothing to do with the charset meta tag (which I set, as I described in my question), and also not with encoding/decoding database results. The methos in the accepted answer works well for me, and that solution is not mentioned in the would-be-duplicate.

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • Or, if that's too general: [German Umlauts in strftime date-formatting - correct utf-8 encoding?](https://stackoverflow.com/questions/12680716/german-umlauts-in-strftime-date-formatting-correct-utf-8-encoding) – miken32 Jan 23 '19 at 20:38
  • @miken32 Okay, the latter one might apply, but the answer there is different from the answer below which works well for me. – Johannes Jan 23 '19 at 20:43
  • Check the answer at the bottom of the page. – miken32 Jan 23 '19 at 20:45

1 Answers1

1

Use utf8_encode.

$date_german = strftime("%e. %B %Y", $timestamp_start);
$date_german = utf8_encode($date_german);
NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • @johannes Also check the list of available locales on the server system. There may be a specific locale that already supports utf-8. See for example this answer: https://stackoverflow.com/a/18229644/101087 – NineBerry Jan 23 '19 at 20:37
  • This is a *bad* solution. It only works if the locale happens to use ISO-8859-1 encoding, and breaks the same way in all other circumstances. – deceze Jan 23 '19 at 20:51
  • Today the PHP version changed, and unfortunately that method doesn't work anymore now. I got it to work by including `setlocale(LC_TIME, "de_AT.UTF-8");` and then `$date_german = strftime("%e. %B %Y", $timestamp_start);` – Johannes Mar 16 '19 at 21:24