Using MySQL's TIMESTAMP vs storing timestamps directly

前端 未结 3 414
情深已故
情深已故 2020-11-30 22:18

I\'m in a dilemma about saving date and time values in MySQL\'s TIMESTAMP format vs in a custom UNSIGNED INT format. The main considerations here are speed of retrieval, app

3条回答
  •  眼角桃花
    2020-11-30 22:59

    Arguments for TIMESTAMP

    • It implicitly stores data in UTC time zone. No matter what your session time-zone is. Useful if you need to use different time zones.
    • You can have automated timestamping columns using DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP (one column per table only until MySQL 5.6.5)
    • You can use datetime function for date comparison, addition, subtraction, range lookup etc, without the need to use FROM_UNIXTIME() function - it will make it easier to write queries that can use indexes
    • In PHP

      >> date('Y-m-d h:i:s',4294967295);
      '1969-12-31 11:59:59'
      

      so the range is in fact the same

      • You can still retrieve integer unix timestamp with no additional overhead using UNIX_TIMESTAMP() function: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

    When UNIX_TIMESTAMP() is used on a TIMESTAMP column, the function returns the internal timestamp value directly, with no implicit “string-to-Unix-timestamp” conversion

提交回复
热议问题