2

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.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Will
  • 21
  • 1
  • This massive SQL, rubbish, and "SELECT * FROM times WHERE date <= date('.$date.') LIMIT 0,3 ORDER BY date ASC" – Will Aug 30 '10 at 10:55

2 Answers2

3

you can use DATEDIFF + ABS

SELECT ABS(DATEDIFF(myfield,'2010-04-27')) AS diff FROM mytable ORDER BY diff LIMIT 5;
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
2

you could also query the difference eg. something like

abs(datediff(date, $date))

then order by this

Adam Butler
  • 3,023
  • 5
  • 35
  • 40