I need to create a timestamp field for a table who's rows need to expire after a certain amount of time. If I use the following:
`timestamp` TIMESTAMP DEFAULT NOW(),
It shows the time in a human readable format, it would be a lot easier if I could have it in epoch time so I can calculate with seconds. Is there a way I can create a field that will display the current time when a row is created in epoch time by default? Thanks!
You may want to use the UNIX_TIMESTAMP()
function in your SELECT
statements, as in the following example:
CREATE TABLE test_tb (`timestamp` TIMESTAMP DEFAULT NOW());
INSERT INTO test_tb VALUES (DEFAULT);
SELECT UNIX_TIMESTAMP(`timestamp`) epoch, `timestamp` FROM test_tb;
+------------+---------------------+
| epoch | timestamp |
+------------+---------------------+
| 1281834891 | 2010-08-15 03:14:51 |
+------------+---------------------+
1 row in set (0.00 sec)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With