将月份名称格式化为本地语言



我做了一个预订系统,现在我有一个月名称的问题。在这种情况下,它是英语的,我想用葡萄牙语。

我尝试在mktime语句中使用setlocale(LC_ALL, 'pt_BR')strftime

setlocale(LC_ALL, 'pt_BR');

// Create array containing abbreviations of days of week.
$daysOfWeek = array('Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado');
// What is the first day of the month in question?
$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
// How many days does this month contain?
$numberDays = date('t',$firstDayOfMonth);
// Retrieve some information about the first day of the
// month in question.
$dateComponents = getdate($firstDayOfMonth);
// What is the name of the month in question?
$monthName = $dateComponents['month'];
// What is the index value (0-6) of the first day of the
// month in question.
$dayOfWeek = $dateComponents['wday'];
// Create the table tag opener and day headers

$datetoday = date('Y-m-d');



$calendar = "<table class='table table-bordered'>";
$calendar .= "<center><h2>$monthName $year</h2>";
$calendar.= "<a class='btn btn-xs btn-primary' href='?month=".date('m', mktime(0, 0, 0, $month-1, 1, $year))."&year=".date('Y', mktime(0, 0, 0, $month-1, 1, $year))."'>Mês Anterior</a> ";

$calendar.= " <a class='btn btn-xs btn-primary' href='?month=".date('m')."&year=".date('Y')."'>Atual Mês</a> ";

$calendar.= "<a class='btn btn-xs btn-primary' href='?month=".date('m', mktime(0, 0, 0, $month+1, 1, $year))."&year=".date('Y', mktime(0, 0, 0, $month+1, 1, $year))."'>Próximo Mês</a></center><br>";

正如您在$dayOfWeek中看到的,它具有从星期日到星期六的日名,并且在按钮中文本分别包含前一个月,当前月份和下一个月。

谁能帮我在哪里插入strftime翻译月名?我在每个地方都试过了,但总是出现错误或日历停止工作。

:因此,我在代码中添加了一些行我仍然无法翻译月份名

// What is the name of the month in question?
$monthName = $dateComponents['month'];
setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
echo strftime('%B %Y', strtotime($monthName));

我得到的是三月而不是三月。

我使它工作,但一些特殊字符,如ç不工作:

mar�o 2021
<<p>解决方案/strong>:Utf8_encode解决了问题
// What is the name of the month in question?
$monthName = $dateComponents['month'];
setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
echo utf8_encode(strftime('%B %Y', strtotime($monthName)));

首先,如果没有特定的原因,您不应该使用LC_ALL,因为您只想更改LC_TIME。因此,您应该使用setlocale(LC_TIME, 'pt_BR');

也不要忘记设置您的默认时区:

date_default_timezone_set('America/Sao_Paulo');

你还说,你总是会得到一个错误。为了帮助你,我们需要这个错误信息。

最新更新