Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert timestamp into a database + 7 days

Tags:

sql

mysql

I am trying to create a table in MySQL that has a timestamp field that is 7 days ahead of now() is there a 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?

like image 290
ard Avatar asked Mar 31 '11 18:03

ard


People also ask

How do I add 3 days to a date in SQL?

SQL Server DATEADD() Function The DATEADD() function adds a time/date interval to a date and then returns the date.

How do I add a timestamp to a column in Oracle?

You can use the below code: insert into tablename (timestamp_value) values (TO_TIMESTAMP(:ts_val, 'YYYY-MM-DD HH24:MI:SS')); If you need the current timestamp to be inserted then use the following code: insert into tablename (timestamp_value) values (CURRENT_TIMESTAMP);


2 Answers

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.

like image 164
ceejayoz Avatar answered Sep 19 '22 14:09

ceejayoz


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.

like image 35
Tyson of the Northwest Avatar answered Sep 20 '22 14:09

Tyson of the Northwest