Perad Posted July 3, 2008 Share Posted July 3, 2008 I have a datetime column in the mysql database. I am trying to get posts from the last 2 days. Do I have to convert the time into the db format? If so is there a dedicated function to do this or have I got to create it with the date function. Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted July 3, 2008 Share Posted July 3, 2008 strtotime("-2 days"); Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 3, 2008 Share Posted July 3, 2008 Use the SUBDATE() function. SELECT * FROM posts WHERE date >= DATESUB(NOW(), INTERVAL 2 DAYS); That should work I think. Quote Link to comment Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 I think it's SELECT * FROM posts WHERE date >= DATE_SUB(NOW(), INTERVAL 2 DAY); Also, I highly recommend doing date comparisons either all in SQL or all in PHP, since the times (on a badly configured server) may be different in the two contexts. I've seen it happen before. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 3, 2008 Share Posted July 3, 2008 @TransmogriBenno: Indeed it is. I was going to write SUBDATE instead of DATESUB but I wasn't paying attention. Both SUBDATE and DATE_SUB are valid functions though. @_@ Thanks for that correction. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.