UNIX Timestamp to MySQL DATETIME UNIX Timestamp to MySQL DATETIME mysql mysql

UNIX Timestamp to MySQL DATETIME


Remember to test it before using it for real, this is written from memory but should give you a good idea.

ALTER TABLE `stats` CHANGE `time` `unix_time` int(11) NOT NULL // rename the old columnALTER TABLE `stats` ADD `time` DATETIME NOT NULL // create the datetime columnUPDATE `stats` SET `time`=FROM_UNIXTIME(unix_time) // convert the dataALTER TABLE `stats` DROP `unix_time` // drop the old unix time column


  1. use alter table to create a new column (eg. time2) with the datetime type in the same table
  2. update stats set time2=from_unixtime(time);
  3. use alter table to a) delete the time column, and b) rename the time2 to time.


ALTER TABLE `stats`MODIFY COLUMN `time` timestamp NULL DEFAULT '0000-00-00 00:00:00' AFTER `id`;ALTER TABLE `stats`MODIFY COLUMN `time` datetime NULL DEFAULT '0000-00-00 00:00:00' AFTER `id`;