1

I am building an application in php which handles Competitions. So I have start date and an end date. In the MySQL database I can set the default value of the start date to CURRENT_TIMESTAMP. Similarly, can I set a default value for the end date with a value like CURRENT_TIMESTAMP plus, say, 3 days? Any suggestion on this is more than welcome.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • Possible duplicate of [Can I use a function for a default value in MySql?](http://stackoverflow.com/questions/270309/can-i-use-a-function-for-a-default-value-in-mysql) – frlan Dec 16 '15 at 20:28
  • Is there a specific reason you prefer to have MySQL determine the timestamp? Might it not be easier to use PHP to determine the current timestamp and add time to it (which is pretty easy with a DateTime object), and explicitly insert the adjusted datetime value? – Redbeard011010 Dec 16 '15 at 20:38
  • php gets the value from the user and it will be entered into the database. I just want to set a default value just in case there is no input from the user side. It is not necessary I'm just checking if I can do it somehow. – Yeshwanth Venkatesha Dec 17 '15 at 06:40

1 Answers1

0

You should be able to do this with a trigger on the table:

CREATE TABLE test (
    id SERIAL,
    start TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    end DATETIME DEFAULT NULL
);

CREATE TRIGGER ins_update_end
     BEFORE INSERT ON test
     FOR EACH ROW SET NEW.`end` = NOW() + INTERVAL 3 DAY;

INSERT INTO test SET id=12;

SELECT * FROM test;
miken32
  • 42,008
  • 16
  • 111
  • 154