php 如何更改 created_at 和 updated_at 值 laravel 的默认格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24441395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:19:08  来源:igfitidea点击:

How to change default format at created_at and updated_at value laravel

phplaravel-4timestampeloquent

提问by saiful408

I am new in Laravel. I am creating a application with laravel. When i creating a post then the values of "created_at" and 'updated_at" are look like this:

我是 Laravel 的新手。我正在使用 Laravel 创建一个应用程序。当我创建帖子时,“created_at”和“updated_at”的值如下所示:

2014-06-26 04:07:31
2014-06-26 04:07:31

But i want this values without time look like this :

但我希望没有时间的这个值看起来像这样:

2014-06-26
2014-06-26

Only date without time.

只有日期没有时间。

Is it possible ???

是否可以 ???

Please help me.

请帮我。

My Post Model:

我的帖子模型:

<?php

class Post extends \Eloquent {

    protected $fillable = array('title', 'body', 'meta','reporter', 'slug', 'image','top');

    public static $rules = array(
        'title'=>'required|min:2',
        'body'=>'required|min:20',
        'reporter'=> 'required|min:2',
        'image'=>'image|mimes:jpeg,jpg,bmp,png,gif'
    );

    public function categories()
    {
        return $this->belongsToMany('Category');
    }

}

My Post Controller store:

我的邮政控制器商店:

public function store()
{
    $validator = Validator::make(Input::all(), Post::$rules);

    if ($validator->passes()) {
        $post = new Post;
        $post->title = Input::get('title');
        $post->body = Input::get('body');
        $post->reporter = Input::get('reporter');
        $post->meta = Input::get('meta');
        $post->slug = Input::get('title');
        $post->top = Input::get('top');

        $image = Input::file('image');
        if ($image) {
            $filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
            Image::make($image->getRealPath())->resize(250, 145)->save('public/images/postimages/'.$filename);
            $post->image = 'images/postimages/'.$filename;
        }


        $categories = Input::get('categories');

        $post->save();

        $post->categories()->sync($categories);

        return Redirect::route('admin.posts.index')
            ->with('message', 'Product Created');
    }

    return Redirect::back()
        ->with('message', 'Something went wrong')
        ->withErrors($validator)
        ->withInput();
}

Please Help me.

请帮我。

回答by The Alpha

In your Postmodel add two accessor methods like this:

在您的Post模型中添加两​​个访问器方法,如下所示:

public function getCreatedAtAttribute($date)
{
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}

public function getUpdatedAtAttribute($date)
{
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}

Now every time you use these properties from your model to show a date these will be presented differently, just the date without the time, for example:

现在,每次您使用模型中的这些属性来显示日期时,这些都会以不同的方式呈现,只是不带时间的日期,例如:

$post = Post::find(1);
echo $post->created_at; // only Y-m-d formatted date will be displayed

So you don't need to change the original type in the database. to change the typein your database you need to change it to Datefrom Timestampand you need to do it from your migration (If your using at all) or directly into your database if you are not using migration. The timestamps()method adds these fields (using Migration) and to change these fields during the migration you need to remove the timestamps()method and use date()instead, for example:

所以你不需要改变数据库中的原始类型。要更改type数据库中的 ,您需要将其更改为DatefromTimestamp并且您需要从您的迁移(如果您使用)或直接到您的数据库中(如果您不使用迁移)进行更改。该timestamps()方法添加这些字段(使用迁移),并在迁移期间更改这些字段,您需要删除该timestamps()方法并使用date(),例如:

$table->date('created_at');
$table->date('updated_at');

回答by Maged Hamid

{{ $post->created_at }}

will return '2014-06-26 04:07:31'

将返回 '2014-06-26 04:07:31'

The solution is

解决办法是

{{ $post->created_at->format('Y-m-d') }}

回答by Loren

Laravel 4.x and 5.0

Laravel 4.x 和 5.0

To change the time in the database use: http://laravel.com/docs/4.2/eloquent#timestamps

要更改数据库中的时间,请使用:http: //laravel.com/docs/4.2/eloquent#timestamps

Providing A Custom Timestamp Format

提供自定义时间戳格式

If you wish to customize the format of your timestamps, you may override the getDateFormat method in your model:

如果您希望自定义时间戳的格式,您可以覆盖模型中的 getDateFormat 方法:

class User extends Eloquent {

    protected function getDateFormat()
    {
        return 'U';
    }

}

Laravel 5.1+

Laravel 5.1+

https://laravel.com/docs/5.1/eloquent

https://laravel.com/docs/5.1/eloquent

If you need to customize the format of your timestamps, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:

如果您需要自定义时间戳的格式,请在模型上设置 $dateFormat 属性。此属性确定日期属性在数据库中的存储方式,以及模型序列化为数组或 JSON 时的格式:

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

回答by Amit Kumar

You can also try like this.

你也可以这样试试。

Use Carbon\Carbon;


$created_at = "2014-06-26 04:07:31";

$date = Carbon::parse($created_at);

echo $date->format("Y-m-d");

回答by nwaweru

If anyone is looking for a simplesolution in Laravel 5.3:

如果有人在 Laravel 5.3 中寻找简单的解决方案:

  1. Let default timestamps()be saved as is i.e. '2016-11-14 12:19:49'
  2. In your views, format the field as below (or as required):

    date('F d, Y', strtotime($list->created_at))
    
  1. 让默认值timestamps()按原样保存,即 '2016-11-14 12:19:49'
  2. 在您的视图中,将字段格式化如下(或根据需要):

    date('F d, Y', strtotime($list->created_at))
    

It worked for me very well for me.

它对我非常有效。

回答by MR_AMDEV

In laravel 6the latest one can easily add in the "APP/user.php" model:

Laravel 6 中,最新的可以很容易地添加到“APP/user.php”模型中:

/**
 * The storage format of the model's date columns.
 *
 * @var string
 */
protected $dateFormat = 'U';

And in schema one can add

在模式中可以添加

    $table->integer('created_at')->nullable();
    $table->integer('updated_at')->nullable();