I have a BirthDate column in MySQL database table to store user's date of birth. Now I have form in html/php with two fields(1. Age From 2. Age To).
If user want to get all users where their ages are between 10 years and 20 years, Is it possible to do this with MySQL query using BirthDate column.
Thanks
We can find the age of a person from their date of birth by using this formula: SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),'DATE_OF_BIRTH')), '%Y') + 0 AS age; In the above formula, we are first finding the difference between the current date (NOW()) and the date of birth (in YYYY-MM-DD) using the DATEDIFF() function.
MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' . The DATETIME type is used for values that contain both date and time parts.
The following is the output. The following is the query to format the date to YYYY-MM-DD. mysql> select str_to_date(LoginDate,'%d. %m.
Using SQL's DateDiff() for Age The short solution is to use the built-in function DATEDIFF( ) where you are able to find the year difference between two dates.
Your query would look similar to this:
SELECT * FROM your_table WHERE YEAR(CURDATE())-YEAR(BirthDate) BETWEEN 10 AND 20;
You can get like this
SELECT column1, column2,
DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(dob)), '%Y') + 0 as age
WHERE age >10 AND age <20 ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With