Laravel 语言环境不采用其他日期格式

时间:2021-02-05 22:23:51

标签: php laravel locale

在 Laravel 中,我在格式化日期时遇到了格式化问题:

utf8_encode(strftime("%d %B %Y", $c_d->getTimestamp()))

$c_d->format("d/B/Y")

它是英文格式。在 config/app.php 中,我设置了 'locale' => 'fr'。我可以在 date 上使用法语日期格式的唯一方法是将 setlocale(LC_TIME, 'fr_FR'); 放在每个格式调用的前面,这很麻烦。

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

我建议使用 Laravel 已经附带的很棒的 Carbon library。 Carbon 检测 config/app.php 文件中设置的语言并相应地格式化日期。

您可以使用 now() helper 或像这样使用 Carbon 实例来尝试:

use Carbon\Carbon;

// Output example: Dec 25, 1975
$myDate = Carbon::createFromTimestamp($ts)-> toFormattedDateString();