【问题标题】:Inserting date and time as "2021-05-13T12:31" in database?在数据库中插入日期和时间为“2021-05-13T12:31”?
【发布时间】:2021-08-13 13:27:28
【问题描述】:

我的表单中有一个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?

【问题讨论】:

    标签: database laravel laravel-migrations


    【解决方案1】:

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

    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();
        });
    }
    

    【讨论】:

      【解决方案2】:

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

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

      然后就可以使用了

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 2015-09-05
        • 2013-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-06
        相关资源
        最近更新 更多