在php中以法语显示月份名称?

时间:2019-07-11 09:24:27

标签: php wordpress date locale french

我正在处理如下所示的php代码:

<?php <time datetime="<?php  echo esc_attr(date_format($ts, 'H:i d-m-Y'))  ?>"
data-timezone="<?php  echo esc_attr($tz_param)  ?>"><?php echo esc_html(date_format($ts, 'F j  H:i')) ?></time> ?> // Line A 

A行在网页上返回以下日期:

July 10 21:30

print_r($ts)打印:

DateTime Object
(
    [date] => 2019-07-10 21:30:00.000000
    [timezone_type] => 3
    [timezone] => America/New_York
)
July 10  21:30

问题陈述:

我想知道应该对上面的 A行中的php代码进行哪些更改,以便当页面为法语时,它应该以法语返回日期

这是我尝试过的,但是它仍然以英语返回日期

<?php if(ICL_LANGUAGE_CODE=='fr'){
setlocale(LC_TIME, 'fr_FR');
?>
<time datetime="<?php echo esc_attr(date_format($ts, 'H:i d-m-Y')) ?>"
   data-timezone="<?php echo esc_attr($tz_param) ?>"><?php echo strftime(esc_html(date_format($ts, 'F j  H:i'))) ?></time> // Line B
<?php } ?>
上面的

B行仍返回英语。

6 个答案:

答案 0 :(得分:3)

使用strftime:http://php.net/manual/en/function.strftime.php

<?php
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %d.%M.%Y and");

(不确定那%d。%M.%Y,但您可以阅读文档)

  

或使用Carbon包裹

本地化http://carbon.nesbot.com/docs/#api-localization

Carbon::setLocale('fr');
Carbon::yesterday()-> diffForHumans();

答案 1 :(得分:2)

假设您网站的基本语言是法语,则可以像这样使用内置的WordPress功能date_i18n()

echo date_i18n( 'F j  H:i', $ts ); //Assuming $ts is your time stamp

如果$ts实际上是一个日期对象,则必须像这样首先获取时间戳:

echo date_i18n( 'F j  H:i', $ts->getTimeStamp() ); //Assuming $ts is a dateTime object

如果您正在努力解决时区差异(即返回的时间提前/落后几个小时),则需要将函数与get_date_from_gmt结合使用,如下所示:

$correct_date = get_date_from_gmt(date_format($ts, 'Y-m-d H:i:s')); //gets the date in your current timezone
echo date_i18n( 'F j  H:i', strtotime($correct_date) );

答案 2 :(得分:1)

来自php doc的参考

  

注意:   setlocale()的返回值取决于PHP正在运行的系统。它完全返回系统setlocale函数返回的内容。

setlocale(LC_ALL, 'fr_FR');

echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978)); //vendredi 22 d�cembre 1978

答案 3 :(得分:1)

您应该使用intl扩展名:

$ts = new DateTime();
$formatter = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::LONG);
$formatter->setPattern('MMMM d  HH:mm');

echo $formatter->format($ts);

列出了setPattern的不正确格式here

答案 4 :(得分:0)

解决该问题:

<?php 
if (date("m", time() ) === "01" ) { print "Janvier"; }
if (date("m", time() ) === "02" ) { print "Février"; }
if (date("m", time() ) === "03" ) { print "Mars"; }
if (date("m", time() ) === "04" ) { print "Avril"; }
if (date("m", time() ) === "05" ) { print "Mai"; }
if (date("m", time() ) === "06" ) { print "Juin"; }
if (date("m", time() ) === "07" ) { print "Juillet"; }
if (date("m", time() ) === "08" ) { print "Août"; }
if (date("m", time() ) === "09" ) { print "Septembre"; }
if (date("m", time() ) === "10" ) { print "Octobre"; }
if (date("m", time() ) === "11" ) { print "Novembre"; }
if (date("m", time() ) === "12" ) { print "Décembre"; }
?>

我尝试使用setlocale()进程,但遇到了与您相同的问题。打印输出仍为英文。这可能是一个“老派”解决方案,但它很简单,并且可以正常工作。

答案 5 :(得分:0)

来自http://php.net/manual/en/function.date.php

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
相关问题