Jump to content
Sign in to follow this  
Terminaxx

NOW() + Days

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.

Edited by Terminaxx

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

To the last point, what happens when your enddate is now 5 days instead of 3? Store the interval instead and only do the math when it's necessary.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • 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.