Laravel - diffForHumans 德语

kam*_*hen 3 php datetime laravel php-carbon laravel-5

我正在尝试在 laravel 5.2 中created_at使用 Carbon 的diffForHumans()德语显示属性。

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();
}
Run Code Online (Sandbox Code Playgroud)

dd($value);返回"2017-03-29 17:31:52"

风景

@foreach($posts as $post)
    <small>{{ $post->getCreatedAtAttribute($post->created_at) }}</small>
@endforeach
Run Code Online (Sandbox Code Playgroud)

错误

DateTime::__construct(): 无法在位置 0 (v) 解析时间字符串 (vor 3 Tagen):在数据库中找不到时区


我将非常感谢任何形式的帮助!

小智 5

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

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

        \Carbon\Carbon::setLocale(config('app.locale'));
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此设置显示数据:vor 3 Tagen而不是3 days ago