用其他语言翻译日期名称

问题描述 投票:1回答:1

大家好,我是PHP的新手...使用此功能,我具有两个日期之间的日期名称,但是如果不破坏它,我无法将其翻译成意大利语。有人能帮我吗?我试图在这两种模式下使用strftime(),但它给了我错误:strftime()期望参数2为int,给定对象,我已经完成了var_dump的日期操作,它是object,我需要将其强制转换为这不是我的问题的答案吗?

$from_date = new DateTime($from_date);
    $to_date = new DateTime($to_date);

    for ($date = $from_date; $date <= $to_date; $date->modify('+1 day')) {
       echo $date->format('l') . "\n";
       //echo strftime("%A", $date); error : strftime() expects parameter 2 to be int, object given
      //echo $formatted_time = strftime("%a %e.%l.%Y", $date->format('l')); // same
      //var_dump($date);  object(DateTime)
      }

输出:星期三星期四星期五星期五

php
1个回答
0
投票

如果您正确设置语言环境setlocale(LC_TIME, 'it_IT.UTF-8');,这将为您工作。我还对您的代码进行了一些修改,以使其更具可读性和简洁性。

<?php
setlocale(LC_TIME, 'it_IT.UTF-8');
$period = new DatePeriod(new DateTime('2020-04-07'),new DateInterval('P1D'),new DateTime('2020-04-18'));
foreach ($period as $key => $value) {
  echo strftime("%A", $value->getTimestamp()).PHP_EOL;     
}
?>

工作演示: https://3v4l.org/sYirG

© www.soinside.com 2019 - 2024. All rights reserved.