verdrm Posted February 24, 2008 Share Posted February 24, 2008 Is there a way to SELECT data WHERE date is closest to today's date? I use the following to format dates: $date = date("D, M dS, Y"); Link to comment https://forums.phpfreaks.com/topic/92695-where-date-is-closest-to/ Share on other sites More sharing options...
Northern Flame Posted February 24, 2008 Share Posted February 24, 2008 i think it'd be easier if you used NOW() instead of a custom date. Link to comment https://forums.phpfreaks.com/topic/92695-where-date-is-closest-to/#findComment-474966 Share on other sites More sharing options...
verdrm Posted February 24, 2008 Author Share Posted February 24, 2008 OK, but how does that help me if I want to retrieve, let's say, 5 records closest to today's date? OR--better thought, what about 5 records with the highest ID...so instead of by date what about by the auto_increment ID? Link to comment https://forums.phpfreaks.com/topic/92695-where-date-is-closest-to/#findComment-474968 Share on other sites More sharing options...
tapos Posted February 24, 2008 Share Posted February 24, 2008 Use 'order by' to sort out the data according the date then use LIMIT to take just 5 row. Link to comment https://forums.phpfreaks.com/topic/92695-where-date-is-closest-to/#findComment-474971 Share on other sites More sharing options...
Northern Flame Posted February 24, 2008 Share Posted February 24, 2008 well if you inserted NOW() you can either: mysql_query("SELECT * FROM table WHERE date > (NOW() - INTERVAL 10 DAY)") // retrieves entries from last 10 days or mysql_query("SELECT * FROM table ORDER BY date DESC LIMIT 10") // retrieves last 10 entries Link to comment https://forums.phpfreaks.com/topic/92695-where-date-is-closest-to/#findComment-474972 Share on other sites More sharing options...
Barand Posted February 24, 2008 Share Posted February 24, 2008 If this is the date format in your table $date = date("D, M dS, Y"); then it's totally useless for any sort or date comparison Link to comment https://forums.phpfreaks.com/topic/92695-where-date-is-closest-to/#findComment-475010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.