php strftime 法语字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8993971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php strftime French characters
提问by ianckc
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:
我在 utf-8 中有我的字符集:
<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?
有没有办法来解决这个问题?
回答by jeroen
This seems to be a problem / bug with the strftime
function.
这似乎是该strftime
功能的问题/错误。
You can solve it using:
您可以使用以下方法解决它:
$date_string = utf8_encode(strftime('%d %B %Y', strtotime($post->post_date)));
回答by Andre F
The Content-Type header needs to set the code page to UTF-8.
Content-Type 标头需要将代码页设置为 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 echoor printmake sure you set it early in the page.
由于一旦使用echo或print将任何内容输出到页面后就无法更改标题,因此请确保在页面的早期设置它。
The ASCII code page is fully contained in UTF-8 not vice-versa.
ASCII 代码页完全包含在 UTF-8 中,反之亦然。
Replace the UTF-8header with the ASCIIone and you'll see what happens when the characters aren't included in the current code page.
将UTF-8标头替换为ASCII标头,您将看到当当前代码页中不包含这些字符时会发生什么。
<?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));
?>
回答by deceze
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, e.g.:
语言环境有不同的编码!您将您的网站宣传为使用 UTF-8,但strftime
没有返回 UTF-8 编码的字符串,因为您选择的区域设置不是 UTF-8 区域设置。检查你的系统你有什么语言环境,例如:
$ 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, e.g.:
然后选择您的语言环境的 UTF-8 变体,例如:
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.
如果您的语言环境没有可用的 UTF-8 变体,请咨询您操作系统的帮助系统如何安装它,或者在 PHP 中进行编码转换。
回答by Limitless isa
<?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
// 11.06.2015 - Per?embe
回答by MichaelC
If you display your page with utf8 encoding, you want to get utf8 out of strftime.
如果您使用 utf8 编码显示您的页面,您希望从 strftime 中获取 utf8。
If php's charset is utf8, then you're cooking. If not, you can :
如果 php 的字符集是 utf8,那么你就是在做饭。如果没有,您可以:
utf8_encode()
the output of strftime.append
'.utf8'
to your locale statement if this locale is installed on your system, as insetlocale(LC_ALL, 'fr_FR.utf8')
change php's default charset, by putting the line
AddDefaultCharset UTF-8
in yourphp.ini
or your.htaccess
utf8_encode()
strftime 的输出。'.utf8'
如果您的系统上安装了此语言环境,请附加到您的语言环境语句,如setlocale(LC_ALL, 'fr_FR.utf8')
更改 php 的默认字符集,通过将行
AddDefaultCharset UTF-8
放在您php.ini
或您的.htaccess
回答by makriria
Have you added this on header?
你有没有在标题上添加这个?
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
回答by AymDev
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) :
有这个问题,但是header()
, utf8_encode()
&setlocale()
不起作用,我们不知道实际的编码。这就是我们解决它的方法(如果它对任何人有帮助):
// $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_encodeexpects to encode from a ISO-8859-1encoded string only.
注意:utf8_encode期望仅从ISO-8859-1编码字符串进行编码。
Manual:
手动的:
回答by Vasilii Suricov
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.
当然它只适用于一种语言,但在某些情况下就足够了。
回答by Lebeau Da?kini
I think you can use the function:
我认为您可以使用该功能:
echo utf8_encode(strftime('%d %B %Y', strtotime($post->post_date)))
echo utf8_encode(strftime('%d %B %Y', strtotime($post->post_date)))