2017-07-24 39 views
2

我需要laravel中的日期本地化帮助。其实,如果用户选择EN语言,我需要显示格式为YYYY-MM-DD,对于其他我想要DD.MM.YYYY。现在我使用if语句来确定所选语言并基于该语言呈现页面。Laravel中的日期本地化

@if(App::isLocale('en')) 
    <p>{{date("Y-m-d", strtotime($t->StartofBreak))}}</p> 
@else 
    <p>{{date("d.m.Y", strtotime($t->StartofBreak))}}</p> 
@endif 

这在我看来是一个坏的解决方案,因为我有很多与日期字段的网页,并为他们每个人我需要用这个。任何方式来改善这一点?

+0

Laravel使用碳的日期格式,因此,请检查一下碳文档说大约[定位](http://carbon.nesbot.com/docs/#api-localization ) – aynber

回答

3

一个简单的解决方案是使用翻译或配置来定义的日期格式为每个语言研究。

在/ressources/lang/en/app.php:

'date_format' => 'Y-m-d' 

将其设置为每种语言。

然后使用:

<p>{{ date(__('app.date_format'), strtotime($t->StartofBreak)) }}</p> 
+0

工程就像一个魅力。完善! – harunB10

1

尝试用三元条件
这样

<p>{{date((App::isLocale('en'))?"Y-m-d":"d.m.Y", strtotime($t->StartofBreak))}}</p> 
+0

这也适用,但我更喜欢@Vincent Decaux解决方案。但是谢谢你! – harunB10

+0

它的好,欢迎你:) –