php - Laravel - diffForHumans 德语

标签 php laravel datetime laravel-5 php-carbon

我正在尝试在 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/

相关文章:

php - Magento - 检查特定类别是否处于事件状态

Facades 类中的 Laravel 常量

php - 防止多人编辑同一个表单

php - 带有 Laravel 应用程序的自包含 Docker 镜像(无共享卷)

MySQL计算一个数据点为空时的时间差

c# - .net DateTime MaxValue 一旦存储在数据库中就不同了

html - 如何删除 Chrome 27 中日期时间本地 HTML 输入上的蓝色十字?

php - 如何从 Smarty 中访问 Mysql 结果集?

php - mysql_fetch_array问题

php - CURL 结果放入 PHP 数组?