在Laravel上定位日期的最佳方法是什么?

编程入门 行业动态 更新时间:2024-10-28 14:36:50
本文介绍了在Laravel上定位日期的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以这个例子为例:

{{ $article->created_at->format('M') }}

它返回Nov.我需要将此语言本地化为我的语言,因此输出应为Kas

It returns Nov. I need to localise this to my language, so the output should be Kas

请考虑以下事项:

{{ trans("language.{$article->created_at->format('M')}") }} app/lang/tr/language.php -> 'nov' => 'kas'

这看起来像在重新发明轮子,并且在编程上非常糟糕.我确定有一些本地化标准.像这样:

This looks like reinventing the wheel and programmatically pretty terrible. I'm sure there are some localisation standards. Something like:

{{ $article->created_at->format('M')->localiseTo('tr_TR') }}

实现此目标的最佳方法是什么?

What's the best way to achieve this?

推荐答案

使用库作为Laravel-Date,您只需在Laravel应用配置文件中设置应用的语言,并使用其功能将日期格式化为你想要的.

Using a library as Laravel-Date you will just need to set the language of the app in the Laravel app config file and use its functions to format the date as you want.

在/app/config/app.php中设置语言

Set the language in /app/config/app.php

'locale' => 'es',

我发现这个库非常有用而且干净.要使用它,您可以在自述文件库中编写类似示例的内容.我将结果保留为西班牙语.

I've found this library pretty useful and clean. To use it, you can write something like the example in the library readme file. I leave the results in spanish.

echo Date::now()->format('l j F Y H:i:s'); // domingo 28 abril 2013 21:58:16 echo Date::parse('-1 day')->diffForHumans(); // 1 día atrás

这是存储库的链接:

github/jenssegers/laravel-date

要安装此库,您可以按照以下链接中详细说明的指示进行操作:

To install this library you can follow the instructions detailed in the following link:

github/jenssegers/laravel-date#installation

更多推荐

在Laravel上定位日期的最佳方法是什么?

本文发布于:2023-11-24 09:30:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   方法   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!