Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert age to current date

Tags:

php

sqlite

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?

like image 892
Saud Iqbal Avatar asked Jun 03 '12 07:06

Saud Iqbal


2 Answers

Is there any PHP function to convert current age to date

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:

  • before or on today - 18 years
  • after today - 19 years

For a user to be 50 today, his birthday must be:

  • before or on today - 50 years
  • after today - 51 years

Note 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)

$lower = date('Y-m-d', strtotime('today -18 years'));
$upper = date('Y-m-d', strtotime('today -51 years'));
$query = "SELECT FirstName FROM users WHERE dob >= '$lower' AND dob < '$upper';";

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:

$d = new DateTime('today -85 years');
$s = $d->format('Y-m-d'); //1927-06-03 as of 3 June 2012
like image 129
Corbin Avatar answered Nov 09 '22 23:11

Corbin


To convert current age to birthday use the following code: it's working with me correctly

function age_To_Birthday($age) {
    $days = (int)($age * 365); //convert age to days

    $date = new DateTime("today -$days day");
    $birthday = $date->format('Y-m-d');
    return $birthday;
}

Test Results:

echo age_To_Birthday(1)     . "<br>"; //2017-09-09
echo age_To_Birthday(5)     . "<br>"; //2013-09-10
echo age_To_Birthday(1.5)   . "<br>"; //2017-03-11
echo age_To_Birthday(1.25)  . "<br>"; //2017-06-10
echo age_To_Birthday(0.25)  . "<br>"; //2018-06-10
echo age_To_Birthday(0.5)   . "<br>"; //2018-03-11
echo age_To_Birthday(0.75)  . "<br>"; //2017-12-10
like image 34
Khaled EL Debuch Avatar answered Nov 10 '22 00:11

Khaled EL Debuch