如何在现有脚本中使用PHP获取法语月份名称?

时间:2018-01-17 04:39:47

标签: php date french

我使用下面的代码创建未来12个月的选择下拉列表,从本月开始:

$year=date("Y");
//Current Month
$month = date('m');
$dateObj   = DateTime::createFromFormat('!m', $month);
$month0 = $dateObj->format('F'); 
$monthHTML .= "<option value='$month/$year'>$month0 $year</option>";
//next 11 months
for ($i=1; $i<=11; $i++) {
    $month = date('m', strtotime('first day of +'.$i.' month'));
        if($month==01) {
            $year++;
        }
    $dateObj   = DateTime::createFromFormat('!m', $month);
    ${"month".$i} = $dateObj->format('F'); 
    $monthHTML .= "<option value='$month/$year'>".${"month".$i}." $year</option>";
}

创建如下输出:

<select id="default-calendar-month" name="default-month">
            <option value='01/2018'>January 2018</option><option value='02/2018'>February 2018</option><option value='03/2018'>March 2018</option><option value='04/2018'>April 2018</option><option value='05/2018'>May 2018</option><option value='06/2018'>June 2018</option><option value='07/2018'>July 2018</option><option value='08/2018'>August 2018</option><option value='09/2018'>September 2018</option><option value='10/2018'>October 2018</option><option value='11/2018'>November 2018</option><option value='12/2018'>December 2018</option></select>

我想使用相同的代码来获取法语列表,例如......

<select id="default-calendar-month" name="default-month">
            <option value='01/2018'>Janvier 2018</option><option value='02/2018'>Fevrier 2018</option>

等等。

查看其他答案(例如this),我尝试添加:

setlocale(LC_TIME, "fr_FR");

将“strtotime”更改为“strftime”,但这会导致这两个错误。我做错了什么?

Warning: date() expects parameter 2 to be long, string given in /home/mysite/public_html/add.php on line 31

Fatal error: Call to a member function format() on a non-object in /home/mysite/public_html/add.php on line 36

2 个答案:

答案 0 :(得分:2)

<? 
    setlocale (LC_TIME, 'fr_FR.utf8','fra'); 
    echo (strftime("%A %d %B")); 

?>

这是我在SO中找到的另一种方法

$date = str_replace(
    array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'),
    array('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'),
   $date
);

答案 1 :(得分:0)

使用setlocale() (setlocale doc)更改语言区域并使用strftime() (strftime doc)

<?php
setlocale(LC_ALL, 'fr_FR');

echo strftime("%A");
?>