While developing a Facebook Dating app recently, I needed to return records for a date range between minimum and maximum age from users table but the data was in the date format (birthday).
Following select command will calculate the age from the `birthday` column value for you for each record:
SELECT EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`birthday`))))+0 AS age FROM `table`
And if you have to use it in the SELECT statement, then do it like this in yuor select command where `birthday` is the column name for date:
SELECT * FROM `table` WHERE EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`birthday`))))+0 BETWEEN '18' AND '25'
Hope that helps.
Thanks for your examples – these work perfectly and I’ve manually cross-referenced against a separate PHP function that I wrote to calculate ages based on YYYY-MM-DD formatted dates and the results were exactly the same.
As always, the simplest solution is usually the best one 🙂