php strftime French characters

前端 未结 9 1457
眼角桃花
眼角桃花 2020-12-08 10:08

I\'m working on a site where the user can switch between English and French. To output the date of posts.

If the user chooses French I use:

setlocale         


        
相关标签:
9条回答
  • 2020-12-08 10:54

    This seems to be a problem / bug with the strftime function.

    You can solve it using:

    $date_string = utf8_encode(strftime('%d %B %Y', strtotime($post->post_date)));
    
    0 讨论(0)
  • 2020-12-08 10:58

    If you display your page with utf8 encoding, you want to get utf8 out of strftime.

    If php's charset is utf8, then you're cooking. If not, you can :

    • utf8_encode() the output of strftime.

    • append '.utf8' to your locale statement if this locale is installed on your system, as in setlocale(LC_ALL, 'fr_FR.utf8')

    • change php's default charset, by putting the line AddDefaultCharset UTF-8 in your php.ini or your .htaccess

    0 讨论(0)
  • 2020-12-08 10:59

    Had this issue but header(), utf8_encode() & setlocale() weren't working and we did not know the actual encoding. This is how we solved it (if it helps anyone) :

    // $date_start is a DateTime instance
    $month = strftime("%b", $date_start->getTimestamp());
    
    // The default value for the 3rd argument is FALSE, this can cause issues
    $encoding = mb_detect_encoding($month, 'auto', true);
    
    // We can now correctly convert the string to UTF-8
    $converted = mb_convert_encoding($month, 'UTF-8', $encoding);
    

    Note: utf8_encode expects to encode from a ISO-8859-1 encoded string only.

    Manual:

    • DateTime::getTimestamp()
    • mb_detect_encoding()
    • mb_convert_encoding()
    0 讨论(0)
提交回复
热议问题