-6

I have a PHP form which includes 3 fields for person birthday: iBirthday, iBirthMonth (int 1 thru 12), and iBirthYear (int 4 digits)

I am trying to calculate if the person is older than 18 years, when pressing submit, before running the SQL INSERT.

How can i calculate this? I have no idea how to concatenate these 3 int into a date, and compare to current date.

I have some tests that work, for example

//Check if gender is NULL 
if ($iGender == '') { 
$sTopError = gettext('Did not select Gender'); 
$bErrorFlag = true; 
} 

I would like to do a similar test, that says: "Person not above 18 years".

yurividal
  • 167
  • 7

1 Answers1

-1

This function will return amount of years from given date in YYYY-MM-DD format:

function get_age($date)
{
    $today = date('Y-m-d');

    $today  = new DateTime($today);
    $date   = new DateTime($date);

    $diff = $date->diff($today)->format("%Y");

    return (int)$diff;
}

Usage:

echo get_age('2000-02-01');
NXT
  • 1,981
  • 1
  • 24
  • 30
  • Why should the OP "use this"? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Jun 19 '17 at 11:39