views:

48

answers:

2
+2  Q: 

SQL Nearest Date

I need to get a date such as '2010-04-27' as a string in php and find the nearest 5 dates in a table. The date in the table are saved as a date type.

+3  A: 

you can use DATEDIFF + ABS

SELECT ABS(DATEDIFF(myfield,'2010-04-27')) AS diff FROM mytable ORDER BY diff LIMIT 5;
Haim Evgi
Im getting 'is not a valid resource'
Will
check this for "is not a valid resource":http://stackoverflow.com/questions/253378/not-a-valid-mysql-resource
Adam Butler
This doesnt appear to be filling $row.while($row = mysql_fetch_array($result)) {The array row contains no column data
Will
+2  A: 

you could also query the difference eg. something like

abs(datediff(date, $date))

then order by this

Adam Butler
+1 ADAM ,i forgot ABS !
Haim Evgi