我正在尝试在 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 days ago
。
关于php - Laravel - diffForHumans 德语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43168444/