Jun 29, 2015

PHP strftime

It took me hours to debug why a template return null, only when using Japanese locale.
Finally I found out.
strftime often used to format nicely date in Japanese, but it will easily broken in production.

There is a workaround to solve this:

function ensureUtf8($text) {
    return iconv(mb_detect_encoding(
        $text, mb_detect_order(), true), "UTF-8", $text    
    );
}


function _strftime($format, $time = null) {
    return ensureUtf8(strftime($format, $time));
}

No comments:

Post a Comment

New comment