简体   繁体   中英

php strftime French characters

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(LC_ALL, 'fra_fra');

Then to output the date I use:

strftime('%d %B %Y', strtotime($post->post_date));

I have my charset at utf-8 with:

<meta charset="utf-8">

The problem I have is characters like û and others with accents just display as the black diamonds with question marks in.

Is there a way to fix this?

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)));

The Content-Type header needs to set the code page to UTF-8.

header('Content-Type: text/html; charset=UTF-8');

Since you can't change the header once you've output anything to the page with or make sure you set it early in the page.将任何内容输出到页面无法更改标题,因此请确保在页面的早期设置它。

The ASCII code page is fully contained in UTF-8 not vice-versa.

Replace the header with the one and you'll see what happens when the characters aren't included in the current code page.标头替换为标头,您将看到当当前代码页中不包含这些字符时会发生什么。

<?php
header('Content-Type: text/html; charset=UTF-8');
//header('Content-Type: text/html; charset=ASCII');

$myDate = "Feb 23, 2011";

$locale = 'fr_FR.UTF-8';
setlocale(LC_ALL, $locale);
echo strftime('%d %B %Y', strtotime($myDate));  

$locale = 'en_US.UTF-8';
setlocale(LC_ALL, $locale);
echo strftime('%d %B %Y', strtotime($myDate));
?>

Locales come in different encodings! You are advertising your site as using UTF-8, but strftime does not return a UTF-8 encoded string, because the locale you chose is not a UTF-8 locale. Check your system what locales you have, eg:

$ locale -a | grep fr_FR
fr_FR
fr_FR.ISO8859-1
fr_FR.ISO8859-15
fr_FR.UTF-8

Then choose the UTF-8 variant of your locale, eg:

setlocale(LC_ALL, 'fr_FR.UTF-8');

If you do not have a UTF-8 variant of your locale available, either consult the help system of your OS how to install it, or do an encoding conversion in PHP.

<?php
    date_default_timezone_set('Europe/Istanbul');
    setlocale(LC_TIME,"turkish");
    echo date("d.m.Y").' - '.iconv("ISO-8859-9","UTF-8",strftime('%A'));
?>

// 11.06.2015 - Perşembe

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

utf8_encode(strftime('%e %B %Y', $this->createDate->getTimestamp()))

doesn't work for me

    class DateUtil
    {
    const FORMAT_DAY_OF_WEEK        = '%A';
    const FORMAT_HUMANY             = '%e %B %Y';

    private static $monthInGenitiveCase = [
        '01' => 'января',
        '02' => 'февраля',
        '03' => 'марта',
        '04' => 'апреля',
        '05' => 'мая',
        '06' => 'июня',
        '07' => 'июля',
        '08' => 'августа',
        '09' => 'сентября',
        '10' => 'октября',
        '11' => 'ноября',
        '12' => 'декабря'
    ];

    public static function dow(DateTime $date)
    {
        return self::format($date, self::FORMAT_DAY_OF_WEEK);
    }

    public static function humany(DateTime $date)
    {
        return self::format($date, '%e ') .self::$monthInGenitiveCase[self::format($date, '%m')] .self::format($date, ' %Y');
    }

    public static function format(DateTime $date, $format)
    {
        return strftime($format, $date->getTimestamp());
    }

}

Usage

DateUtil::humany($this->createDate)

Ofcourse it works only for one language, but in some cases it's enough.

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:

我认为您可以使用该功能:

echo utf8_encode(strftime('%d %B %Y', strtotime($post->post_date)))

你有没有在标题上添加这个?

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM