繁体   English   中英

php,如何以固定的区域设置/语言格式设置 output 日期?

[英]php, how to output date in a fixed locale/language format?

在 php 脚本中,我需要 output 使用固定语言的日期,例如$date-> format('j F Y', 'it_IT') 我知道我可以使用:

setlocale(LC_ALL, 'it_IT');
$dataItalian = strftime("%e %B %Y", strtotime($myDataObj->format('j F Y')));;

但是strftime已被弃用,我不明白我应该怎么做。

(我已经安装了 php-intl)。

我认为你应该使用 IntlDateFormatter class。

请参阅此处的 php 手册 -> https://www.php.net/manual/en/class.intldateformatter.php

<?php 
function formatDate(Datetime $dateTime, $format = 3, $locale = 3 ): string{

    $fmt = new IntlDateFormatter('it_IT', 3, 3, 'Europe/Rome', 1, 'dd/MM/YYYY');
    return $fmt->format($dateTime);

  }

//an example with Datetime 'now' //

  $localZone = new DateTimeZone('Europe/Rome');
  $yourDate1 = new Datetime('now', $localZone);
  echo formatDate($date1);

我希望这会帮助你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM