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. Link to comment https://forums.phpfreaks.com/topic/113055-mysql-compare-dates-against-strtotime/ Share on other sites More sharing options...
DyslexicDog Posted July 3, 2008 Share Posted July 3, 2008 strtotime("-2 days"); Link to comment https://forums.phpfreaks.com/topic/113055-mysql-compare-dates-against-strtotime/#findComment-580866 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. Link to comment https://forums.phpfreaks.com/topic/113055-mysql-compare-dates-against-strtotime/#findComment-580869 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. Link to comment https://forums.phpfreaks.com/topic/113055-mysql-compare-dates-against-strtotime/#findComment-580875 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. Link to comment https://forums.phpfreaks.com/topic/113055-mysql-compare-dates-against-strtotime/#findComment-580878 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.