微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在数据库中插入日期和时间为“2021-05-13T12:31”?

如何解决在数据库中插入日期和时间为“2021-05-13T12:31”?

我的表单中有一个 datetime 输入,用户将从该输入中选择日期和时间。但是插入时,数据应该只有日期和时间,但T也被插入了?怎么不插入呢? 这是迁移代码

public function up()
{
    Schema::create('competitions',function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('desc');
        $table->float('entry_fees');
        $table->varchar('start_date_time',50);
        $table->Date('end_date_time',50);
        $table->integer('status');
        $table->string('date_time');
        $table->timestamps();
    });
}

名称start_date_timeend_date_time

解决方法

如果它只是日期时间,你不能像这样设置你的列,然后在创建时进行碳分析​​p>

public function up()
{
    Schema::create('competitions',function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('desc');
        $table->float('entry_fees');
        $table->datetime('start_date_time');
        $table->datetime('end_date_time');
        $table->integer('status');
        $table->string('date_time');
        $table->timestamps();
    });
}
,

首先你的列应该是你的表中的 dateTime 类型,然后 你可以用 laravel carbon 改变它的格式

$createdAt = Carbon::parse($item['start_date_time']);

然后你就可以使用

$start_date = $createdAt->format('d/m/Y H:i:s');

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。