Check my math please, Verifying a user is 18

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Check my math please, Verifying a user is 18

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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;
  }
  

?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

np ~ if you want to be really picky you can cut the if statements down to one if statement.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

How? By using "OR"?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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();
Post Reply