Jump to content

NOW() + Days


Terminaxx

Recommended Posts

Hey guys,

 

I am trying to add my databasetable the current timestamp (d-m-Y H:i:s) but adding to it 3 days.

 

I tried this:

mysqli_query($con, "INSERT INTO test (enddate) VALUES ('NOW() + INTERVAL 3 DAY')");

But the time is shown up as "0000-00-00 00:00:00" . I tried to fix it with strtotime but then it says that "NOW()" is invalid.

 

Can someone help me with this issue?

 

Sorry for my english, hope you still understand what i am trying to do.

 

Thanks in advance.

Link to comment
Share on other sites

Look at your query:

'NOW() + INTERVAL 3 DAY'

This is a string. You're trying to insert the literal text “NOW() + ...” as a date value, and that's of course nonsense.

 

What you want to insert is the result of a calculation:

NOW() + INTERVAL 3 DAY

An even smarter approach would be to simply store the current date and then calculate the end date dynamically when it's actually needed.

Link to comment
Share on other sites

Make sure that you consider the difference between NOW() and CURDATE. With NOW() you are getting a timestamp including the time component, whereas with CURDATE you get just the DATE, or in DATETIME equivalence, the very start of the day (12:00am).

 

This may nor may not matter given your application.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.