Laravel created_at格式化

时间:2017-09-20 18:34:20

标签: laravel

我在一个laravel网站工作,我只是想知道是否有办法为用户格式化created_at值,因为它现在是这样的:

2017-09-20 13:41 

但我更愿意将其格式化为易于阅读的格式,例如

20th September 2017 13:41 (or 01:41pm if possible)

任何人都可以帮我吗?

提前致谢

3 个答案:

答案 0 :(得分:2)

Laravel的日期是Carbon对象,具有a wide variety of built-in formats available以及使用format()函数构建your own custom format的能力。

所以:

$foo->created_at->format('jS F Y h:i:s A')

答案 1 :(得分:1)

您没有说明您使用的是哪个版本的Laravel,但如果它是5+,您可以在模型中指定日期格式。这将影响created_at,updated_at和deleted_at(如果使用软删除)属性。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = 'U';
}

https://laravel.com/docs/5.5/eloquent-mutators#date-mutators

答案 2 :(得分:0)

在数据库中是不可能的,但在php中是的 http://carbon.nesbot.com/docs/