strftime() and utf8 under windows

Posted by Paltar on April 30, 2011 in PHP |

Some time ago I had a Problem with the PHP-Function strftime(). I wanted to print the month as German text with strftime('%B');. But the month happened to be ‘März’ (German for march) and the character ‘ä’ would not show up correctly despite the use of setlocale(LC_ALL, 'de_DE.UTF8'); to set encoding to utf8.

After some searching the internet I found the reason: On windows systems the function strftime() just doesn’t use utf8 encoding, no matter what you set via setlocale().

The solution is easy enough. Just encode the return value of strftime() with the function utf8_encode(). This solved the Problem on my development system but the live server was running on Linux. On the Linux server the month was now encoded two times and again the ‘ä’ didn’t show up correctly.

Making the output work on both systems, I had to determine the operating system and only use utf8_encode() on the windows system. So the code now looks like this:

1
2
3
4
$month = strftime('%B');
if (runsOnWindows()) {
    $month = utf8_encode($month);
}

With the function runsOnWindows():

1
2
3
4
5
6
7
function runsOnWindows() {
    php_uname();
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        return true;
    }
    return false;
}

This post is also available in: German

Bookmark and Share

Tags: , , , ,

Copyright © 2010-2019 Paltar's Blog All rights reserved.
Desk Mess Mirrored v1.8.3 theme from BuyNowShop.com.