Page 1 of 1

Check my math please, Verifying a user is 18

Posted: Thu Dec 09, 2004 8:48 pm
by Benjamin
I am tired but I think this is right. I just needed a function to make sure a user was >= 18 years old. Let me know what you think.

Code: Select all

<?php
function ParseTimeStamp($TimeStamp)
  {
  $Year = substr($TimeStamp, 0, 4);
  $Month = substr($TimeStamp, 4, 2);
  $Day = substr($TimeStamp, 6, 2);
  $Hour = substr($TimeStamp, 8, 2);
  if ($Hour >= 12) $Meridiem = 'PM'; else $Meridiem = 'AM';
  if ($Hour > 12) $Hour = $Hour - 12;
  $Minute = substr($TimeStamp, 10, 2);
  $Second = substr($TimeStamp, 12, 2);

  $TimeStamp = array(); // Clear the variable to a null state
  $TimeStamp['Year'] = $Year;
  $TimeStamp['Month'] = $Month;
  $TimeStamp['Day'] = $Day;
  $TimeStamp['Hour'] = $Hour;
  $TimeStamp['Meridiem'] = $Meridiem;
  $TimeStamp['Minute'] = $Minute;
  $TimeStamp['Second'] = $Second;
  $TimeStamp['YYYYMMDD'] = $Year . $Month . $Day;
  return $TimeStamp;
  }

function IsOldEnough($TimeStamp, $DOB, $MOB, $YOB) //current timestamp, day of birth, month of birth, year of birth
  {
  $TimeStamp = ParseTimeStamp($TimeStamp);
  if (($TimeStamp['Year'] - $YOB) > 17){return true;} // if current year - year of birth > 18 return true
  if ((($TimeStamp['Year'] - $YOB) == 17) and ($MOB > $TimeStamp['Month'])){return true;} // if was born after this month 17 years ago
  if ((($TimeStamp['Year'] - $YOB) == 17) and ($MOB = $TimeStamp['Month']) and ($DOB >= $TimeStamp['Day'])){return true;} // if was born this month and on or after this day 17 years ago
  return false;
  }

?>
Ya'all are free to use this in your own scripts if you want.

Posted: Thu Dec 09, 2004 9:40 pm
by hawleyjr
You don't really need the ParseTimeStamp Function. Check out [php_man]date[/php_man]

Code: Select all

<?php

function IsOldEnough($DOB, $MOB, $YOB) //current timestamp, day of birth, month of birth, year of birth
  {
  $TimeStamp = ParseTimeStamp($TimeStamp);
  if ((date("Y") - $YOB) > 17){return true;} // if current year - year of birth > 18 return true
  if (((date("Y") - $YOB) == 17) and ($MOB > date("m"))){return true;} // if was born after this month 17 years ago
  if (((date("Y") - $YOB) == 17) and ($MOB = date("m")) and ($DOB >= date("d"))){return true;} // if was born this month and on or after this day 17 years ago
  return false;
  }
  

?>

Posted: Thu Dec 09, 2004 10:07 pm
by Benjamin
Cool, thank you. I wrote that function to parse timestamps stored in a database, but I guess date() does the same thing better.

Posted: Thu Dec 09, 2004 10:12 pm
by hawleyjr
np ~ if you want to be really picky you can cut the if statements down to one if statement.

Posted: Thu Dec 09, 2004 11:17 pm
by Benjamin
How? By using "OR"?

Posted: Fri Dec 10, 2004 2:03 am
by Weirdan
hmm, why not to use the [php_man]strtotime[/php_man] function? like this:

Code: Select all

$dob = strtotime($_REQUEST['dob']);
$cut_off = strtotime("17 years ago");
if ( $dob > $cut_off )
    die("You're too young ");
else
    do_something();