gpt4 book ai didi

php - laravel迁移中的时间格式?

转载 作者:行者123 更新时间:2023-12-03 11:22:05 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('arena');
$table->timestamp("begin");
$table->timestamps();
});
在您的 Post 模型中,您可以简单地执行以下操作:
protected $casts = [
'begin' => 'date:hh:mm'
];
编辑
如果您不使用 laravel 5.6,您可以使用 Accessors & Mutators 轻松操作数据库上的设置数据或从数据库中获取数据,以便您可以执行以下操作
public function getBeginAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('hh:mm');
}
每次你在你的 Blade 中或像这个 {{ $post->begin }} 这样的任何地方回应它时,它会自动为你改变格式。
希望能有所帮助。

关于php - laravel迁移中的时间格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49925768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com