Tuesday, November 2, 2010

MySQL UNIX_TIMESTAMP howto: date and datetime fields operations (common)

MySQL UNIX_TIMESTAMP examples

• set date to current date for all items having dates bigger than 24 hours in the future
UPDATE table_name SET data = now() WHERE UNIX_TIMESTAMP(data) > UNIX_TIMESTAMP() + 24 * 60 * 60;

• delete all entries older than 30 days (having date smaller than current time minus 30 days)
DELETE FROM table_name WHERE UNIX_TIMESTAMP(data) < UNIX_TIMESTAMP() - 30 * 24 * 60 * 60;

Both above examples make use of UNIX_TIMESTAMP function.

No comments: