Is there any PHP function to convert current age to date in YYYY-MM-DD format. I stored the DOB in YYYY-MM-DD format and I want to run the SQLite query:
SELECT FirstName FROM mytable WHERE DOB >= $MinAge AND DOB <= '$MaxAge';
I searched google to convert age to date format but could not find any function, is there anyone who can help?
No, an age contains less information than a date. There are 365 possibilities for a person to be X years old, but a date of birth maps to exactly one age.
In other words, if I tell you that I’m 20, and ask you to tell me my birthday, you can’t with certainty. If I tell you my birthday, however, you can then, with certainty, tell me my age.
As your column is called DOB, I’ve assumed that you’ve stored the date of birth, not the age. In this situation, it is possible to fetch users falling a certain age range.
Let’s assume that you want to find all users between the ages of 18 and 50 (including 18 and 50).
For a user to be 18 today, his birthday must be:
today - 18 yearstoday - 19 yearsFor a user to be 50 today, his birthday must be:
today - 50 yearstoday - 51 yearsNote that “before or on” and “after” are just “<=” and “>”, respectively.
So, what if you want all users who are between 18 and 50 years old?
You can just take the two outer bounds:
A user’s age is between 18 and 50 iff:
dob <= (today -18 years) and dob > (today - 51 years)Note that if you plan on dates before 1970 (52-ish), you should likely use DateTime instead of date and sttrtotime (I actually only use DateTime in actual code, but date/strtotime make for much briefer examples).
An example of 85 years ago with DateTime: