Blog

MySQL. Script to Convert Old Dates to New

This PHP script will convert the old deprecated MySQL date formats to the new ones.

$file_in = 'prod_20191222.sql';

$file_out = $file_in.'.converted.sql';

 

$replacements = [

    "ENGINE = MyISAM"=>"ENGINE = InnoDB",

    " NOT NULL DEFAULT '0000-00-00 00:00:00'"=>" DEFAULT NULL",

    "'0000-00-00 00:00:00'"=>"NULL",

    " NOT NULL DEFAULT '0000-00-00'"=>" DEFAULT NULL",

    "'0000-00-00'"=>"NULL",

];

 

$contents = file_get_contents($file_in);

foreach($replacements as $key=>$value) {

    $contents = str_replace($key, $value, $contents);

}

file_put_contents($file_out, $contents);