编程语言
首页 > 编程语言> > php-Laravel-diffForHumans德语

php-Laravel-diffForHumans德语

作者:互联网

我试图在Laravel 5.2中使用Carbon的diffForHumans()用德语显示created_at属性.
created_at存储在数据库中的方式为:2017-03-29 17:31:52
该模型

protected $dates = ['created_at', 'updated_at'];

public static function getCreatedAtAttribute($value)
{
    Carbon::setLocale('de');
    return Carbon::parse($value, 'Europe/Berlin')->diffForHumans();
}

dd($value);返回“ 2017-03-29 17:31:52”.
风景

@foreach($posts as $post)
    <small>{{ $post->getCreatedAtAttribute($post->created_at) }}</small>
@endforeach

错误

DateTime::__construct(): Failed to parse time string (vor 3 Tagen) at
position 0 (v): The timezone could not be found in the database

我将非常感谢您提供的任何帮助!

解决方法:

对于德语翻译,我在AppServiceProvider中使用了此自定义:

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Localization Carbon

        \Carbon\Carbon::setLocale(config('app.locale'));
    }
}

使用此设置,将显示数据:vor 3 Tagen而不是3天前.

标签:laravel,laravel-5,php-carbon,datetime,php
来源: https://codeday.me/bug/20191026/1933928.html