Chetan
Chetan

Reputation: 48001

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 compared 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?

Upvotes: 4

Views: 3213

Answers (3)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798546

Use TIMESTAMP DEFAULT CURRENT_TIMESTAMP. This will create a TIMESTAMP field that is set on INSERT, but not on UPDATE.

Upvotes: 5

KJ Saxena
KJ Saxena

Reputation: 21828

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.

Upvotes: 1

Andrew G. Johnson
Andrew G. Johnson

Reputation: 26993

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

Upvotes: 0

Related Questions