Terminaxx Posted July 19, 2017 Share Posted July 19, 2017 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 More sharing options...
Jacques1 Posted July 19, 2017 Share Posted July 19, 2017 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 More sharing options...
Terminaxx Posted July 19, 2017 Author Share Posted July 19, 2017 Thanks for the explanation! Link to comment Share on other sites More sharing options...
Sepodati Posted July 19, 2017 Share Posted July 19, 2017 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. Link to comment Share on other sites More sharing options...
gizmola Posted July 19, 2017 Share Posted July 19, 2017 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.