我正在开发一个用户可以在英语和法语之间切换的网站。输出发布日期。
如果用户选择法语,我使用:
setlocale(LC_ALL, 'fra_fra');
然后输出我使用的日期:
strftime('%d %B %Y', strtotime($post->post_date));
我有我的字符集在utf-8与:
<meta charset="utf-8">
我遇到的问题是,像û这样的字符和其他带有重音的字符在.中显示为带问号的黑钻石
有办法解决这个问题吗?
这似乎是strftime
函数的一个问题/错误。
你可以使用来解决它
$date_string = utf8_encode(strftime('%d %B %Y', strtotime($post->post_date)));
Content-Type头需要将代码页设置为UTF-8。
header('Content-Type: text/html; charset=UTF-8');
由于在使用echo或print将任何内容输出到页面后就无法更改页眉,请确保在页面早期进行设置。
ASCII代码页完全包含在UTF-8中,反之亦然。
将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));
?>
区域设置有不同的编码!您正在宣传您的网站使用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
然后选择您所在地区的UTF-8变体,例如:
setlocale(LC_ALL, 'fr_FR.UTF-8');
如果您的语言环境没有UTF-8变体,请咨询操作系统的帮助系统如何安装它,或者用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'));
?>
//2015年6月11日-Perşembe
如果使用utf8编码显示页面,则需要从strftime中获取utf8。
如果php的字符集是utf8,那么您正在烹饪。如果没有,您可以:
-
utf8_encode()
是strftime的输出。 -
如果您的系统上安装了此区域设置,请将
'.utf8'
附加到您的区域设置语句中,如setlocale(LC_ALL, 'fr_FR.utf8')
中所示 -
通过在
php.ini
或.htaccess
中放入行AddDefaultCharset UTF-8
来更改php的默认字符集
utf8_encode(strftime('%e %B %Y', $this->createDate->getTimestamp()))
对我不起作用
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());
}
}
使用
DateUtil::humany($this->createDate)
当然,它只适用于一种语言,但在某些情况下已经足够了。
你在页眉上添加了这个吗?
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
有此问题,但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);
注意:utf8_encode期望仅从ISO-8859-1编码的字符串进行编码
手动:
- 日期时间::getTimestamp()
- mb_detect_encoding()
- mb_convert_encoding()
我认为您可以使用以下函数:
echo utf8_encode(strftime('%d %B %Y', strtotime($post->post_date)))