【问题标题】:Changing format of column to datetime MySql将列的格式更改为日期时间 MySql
【发布时间】:2020-12-22 12:37:00
【问题描述】:

我遇到了一个问题,我公司的一些驴子用 MySql 创建了一个数据库,我们得到了一个日期列,他将它设置为 varchar。
我们不能建立另一个数据库,因为它至少从 10 年前就已经存在了。 现在我对数据进行了一些处理,尤其是日期(我需要成为日期时间)。当然,日期的格式为 'dd/mm/yyyy h:i',当我尝试将 varchar 转换为日期时间时出现此错误:
#1292 - Incorrect datetime value: '24/08/2020 22:05' for column 'column_name' at row 1
我正在使用 Laravel 和 Mysql

有谁知道我该如何摆脱这个?

【问题讨论】:

  • 你可以使用 Carbon,文档carbon.nesbot.com/docs
  • 当然唯一的方法是遍历数据库并将所有这些转换为格式正确的日期/时间,然后尝试更改列类型。
  • 24/08/2020 22:05 应该是2020-08-24 22:05:01
  • @AlzafanChristian 我需要编写一个查询,我正在搜索一些数据并需要 where 子句中的日期,我正在使用 mysql 提供的 day() 方法,但我不能这样做一个varchar..

标签: php mysql laravel date


【解决方案1】:

好吧,我已经设法将每个“/”更改为“-”。 现在我的日期格式类似于“24-08-2020 22:05” 我只需要在每个日期中添加一些秒数(例如 22:05:00),以便为日期时间设置正确的格式

【讨论】:

  • 它仍然需要反过来 - 2020-08-24 对于 datetime.YYYY-MM-DD 是正确的,而不是 DD-MM-YYYY
  • 参见 STR_TO_DATE()。
  • @Strawberry 我想我会使用 Substring 方法,似乎对我有用,但谢谢:)
  • 对我来说似乎是个错误,但不管你的船是什么。
【解决方案2】:

如果您使用 Laravel,只需使用 migrations... 这将是进行大规模数据库更新的最干净的方式。然后使用他们的模型更新记录,并使用Carbon 处理日期:

<?php

use App\TableModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UpdateColumnDatatype extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Get all the data to reformat it
        $data = TableModel::all();

        Schema::table('table_name', function (Blueprint $table) {
            $table->dropColumn('column_name');          // Removes the old column

            // You need to set a ->default() or make this column ->nullable() or you'll
            // get errors since the data is empty now.
            $table->dateTime('column_name');            // Creates the new datetime
        });

        // Iterate over the old data to update the new rows
        foreach($data as $model) {
            $oldDate = Carbon::createFromFormat('d/m/Y h:i', $model->column_name);
            $newDate = $oldDate->format('Y-m-d H:i:s'); // Format for DateTime

            $obj = TableModel::find($model->id);
            $obj->column_name = $newDate;               // Change the record's data
            $obj->save();                               // Save the changes
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('table_name', function (Blueprint $table) {
            $table->dropColumn('column_name'); // Removes the new column
            $table->string('column_name');     // Creates the old column as a string (varchar equivalent) 
            // Do the same thing to turn the new datetime values back to your old format
            // in case you decide to do a rollback
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 2019-10-04
    相关资源
    最近更新 更多