16

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.

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
matt
  • 42,713
  • 103
  • 264
  • 397

3 Answers3

48

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");
lorem monkey
  • 3,942
  • 3
  • 35
  • 49
  • the suggested "de_DE-UTF-8" or in my case "de_AT.UTF-8", respectively "de_AT.utf8" as seen in the php manual is not available on my hosting (Windows Azure Websites (=PaaS)), I have to use "german_austria" but this is not an UTF8 locale and thus outputs ä's ö's and ü's as � - have you got an Idea how to fix that or how to find out all installed locales with php - its a windows machine running IIS8 but as its just a hosted website, I have no system access. – Daniel Steiner Aug 15 '14 at 16:54
  • 3
    @DanielSteiner As a workaround, wrap your `strftime` call in `utf8_encode`. See https://php.net/manual/en/resourcebundle.locales.php for how to get a list of all supported locales. – Chris Mar 31 '15 at 08:31
  • Works like a charm for me. Thanks. – Paul Beck Apr 07 '17 at 09:14
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
Community
  • 1
  • 1
user1460043
  • 2,331
  • 1
  • 19
  • 26
2

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

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
  • Hmm, doesn't make a difference and I do already have `` in my `head`. The rest of the page (based on Wordpress) works fine with all Umlauts, just this date makes problems. – matt Oct 01 '12 at 20:52
  • Well, thank you, but that's not it either … I tested this `$locale = get_locale(); //setlocale(LC_TIME, $locale); setlocale(LC_ALL, 'de_DE');` So I already had the locale set correctly since my entire blog is in german it has the right locale settings. I tried adding the `setlocale` line exactly like you suggest even a second time but no success. – matt Oct 01 '12 at 21:09
  • hmm .. out of options then. As you see I used exact the same code, and I get correct output. Perhaps a link to your blog? – JvdBerg Oct 01 '12 at 21:13
  • This might be some problem with the Hosting Provider as I just tested it locally on my local-server where everything seems to work fine. Could that be the reason? I wonder why the rest is working then? – matt Oct 02 '12 at 07:37
  • 1
    I just copied and pasted, and at my webserver it is also working. As I said, the locale must exist on the webserver. Perhaps it is not de_DE but de_DE.utf8 ?? – JvdBerg Oct 02 '12 at 07:54
  • 8
    That's it! Thank you very much! I added `setlocale(LC_ALL, 'de_DE.utf8');` and now it works just fine! – matt Oct 02 '12 at 08:15