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
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.
Have you added this on header?
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
I think you can use the function:
echo 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 echo or print make sure you set it early in the page.
The ASCII code page is fully contained in UTF-8 not vice-versa.
Replace the UTF-8 header with the ASCII 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, e.g.:
$ 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.:
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