Jump to content

NOW() + Days


Terminaxx
Go to solution Solved by Jacques1,

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
Link to comment
Share on other sites

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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