Insert timestamp into a database + 7 days
I am trying to create a table in MySQL that has a timestamp field that is 7 days ahead of now()
is there 开发者_StackOverflowa way to do this?
This is my current code
CREATE TABLE tbl_reg
(
reg_id int(7) NOT NULL auto_increment KEY,
user_id int(7) NOT NULL,
registration_key char(50) NOT NULL,
registration_expires timestamp default now()+7days,
stamp_created timestamp default now()
);
Can anyone help as i cant find anything like this on the mysql site and wondered if there was a way to do it?
There are a number of date/time functions in MySQL that will do the trick here.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
registration_expires=DATE_ADD(NOW(), INTERVAL 7 DAY)
You can't set an expression as a default, though - you'll need to do it in your INSERT queries. Notice that even if your expr value is > 1 there is no plural used for the unit value.
Or you could create a view from a query where you add the interval, or when you query the db always add the 7 days interval.
精彩评论