How to store time created in MySQL table?
I want to store the timestamp which a row was inserted into a MySQL table with the row, to be later com开发者_如何学Gopared to other times. What's the best field type for this and what's the best way to insert the row with the timestamp in PHP?
Use TIMESTAMP DEFAULT CURRENT_TIMESTAMP
. This will create a TIMESTAMP field that is set on INSERT, but not on UPDATE.
Use TIMESTAMP as the field type. All TIMESTAMP field types will have CURRENT_TIMESTAMP as the default (which is a alias for NOW())
While adding a record, write ''
to that field - it will take the default time as the value.
Use the DateTime datatype for the column, the NOW() function to set the current time and the UNIX_TIMESTAMP() function if you need to compare it using PHP
精彩评论