简体   繁体   中英

unix timestamp in php/mysql installation - best MySQL datatype to store this as?

I'm trying to figure out the best datatype and size in my DB to store unix timestamps...

In otherwords:

INT(32)

etc...

Thanks for any tips.

TIMESTAMP is a pretty straight-forward choice. It's implemented as a UNIX timestamp internally, but externally it appears as a date string (eg "1970-01-01 00:00:01").

EDIT: To migrate an INT to a timestamp, where time_test is the table and ts is the original column:

ALTER TABLE time_test ADD ts_new TIMESTAMP;
UPDATE time_test SET ts_new = FROM_UNIXTIME(ts);
ALTER TABLE time_test DROP ts;
ALTER TABLE time_test CHANGE COLUMN ts_new ts TIMESTAMP;

You may have to tweak it slightly if you care about the column order.

MySql supports Unix timestamps directly as the TIMESTAMP data type.

You have the normal limitations; dates must be from 1 Jan 1970 until 19 Jan 2038.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM