Laravel 迁移中的时间格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49925768/
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-09-14 17:39:39  来源:igfitidea点击:

Time format in laravel migration?

phplaraveltime

提问by Jonas

I want to have an input, where you put a time in EU format like 12:00 or 21:34. (hh:mm) How do I do that?

我想要一个输入,您可以在其中以 EU 格式输入时间,例如 12:00 或 21:34。(hh:mm) 我该怎么做?

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->string('arena');
    $table->date("h,i"('beginn'));
    $table->timestamps();
});

This is what I have, but it's obviously wrong.

这是我所拥有的,但显然是错误的。

采纳答案by AH.Pooladvand

In laravel 5.6 you have this new feature that you can cast your timestamp so your migration should be like this

在 laravel 5.6 中,你有这个新功能,你可以投射你的时间戳,所以你的迁移应该是这样的

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->string('arena');
    $table->timestamp("begin");
    $table->timestamps();
});

And in your Postmodel you can simply do this:

在您的Post模型中,您可以简单地执行以下操作:

protected $casts = [
    'begin' => 'date:hh:mm'
];

Edit
If you are NOT using laravel 5.6 you can use Accessors & Mutatorsto easily manipulate data on setting on database or getting it from database so you can do something like this

编辑
如果您不使用 laravel 5.6,您可以使用Accessors & Mutators轻松操作数据库上的设置数据或从数据库中获取数据,以便您可以执行以下操作

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

And every time that you echoing it out in your blade or wherever like this {{ $post->begin }}it automatically changes the format for you.

每次您在刀片中或任何类似的地方回显它时,{{ $post->begin }}它都会自动为您更改格式。

Hope that helps.

希望有帮助。

回答by Melvin Hagberg

You could do this with Carbon, which is included in Laravel by default:

你可以用 Carbon 来做到这一点,它默认包含在 Laravel 中:

  1. Set a variable with the current time $date = Carbon::now()
  2. Set hour, for example 22 $date->hour = 22
  3. Set minutes, for example 54 $date->minutes = 54
  4. Finally, set $table->date($date)
  1. 使用当前时间设置变量 $date = Carbon::now()
  2. 设置小时,例如22 $date->hour = 22
  3. 设置分钟,例如 54 $date->minutes = 54
  4. 最后,设置 $table->date($date)

回答by Saurav

Since laravel migration is based on traditional SQL convention, you cannot change the format using migration. This is because SQL datetime itself is formatted as YYYY-MM-DD HH:MM:SS. But you can use Carbon date time format to show the stored date, time or both as per you wish. Refer to this linkfor more on Carbon. Official Link Here

由于 Laravel 迁移基于传统的 SQL 约定,因此您无法使用迁移更改格式。这是因为 SQL 日期时间本身的格式为 YYYY-MM-DD HH:MM:SS。但是您可以根据需要使用 Carbon 日期时间格式来显示存储的日期、时间或两者。有关碳的更多信息,请参阅此链接官方链接在这里

回答by khem raj regmi

You can use $table->times('field_name');

您可以使用 $table->times('field_name');

Schema::create('posts', function (Blueprint $table) {
   $table->increments('id');
   $table->string('title');
   $table->string('arena');
   $table->times("begin");
   $table->timestamps();
});