Why won't my check age function work?

Discussion in 'PHP' started by Imozeb, Feb 9, 2010.

  1. #1
    I created a check age function in PHP to make sure the user is at or over thirteen years old. And I don't understand why it isn't working. Please help. :)

    $varday = users brith day
    $varmonth = users brith month
    $varyear = users brith year

    //check age
    if ($showfail == 0 && $bdayerror == "")
    {
    //add zeros if less than 10
    if ($varday < 10){ $varday = "0" . $varday; }
    if ($varmonth < 10){ $varmonth = "0" . $varmonth; }
    //get current day and append bday
    $currentdate = date("U");
    $userbday = $varmonth . "//" . $varday . "//" . $varyear;
    //calculate bday in unix timestamp
    $userage = $currentdate - strtotime($userbday);
    //check if at or older than 13
    if ($userage < (31556925*13))
    {
    //set error message variable
    $bdayerror = "You are under thirteen years old and therefore are not old enough to access this site!";
    $showfail = 1;
    }
    }

    Thanks.

    ~Imozeb
     
    Imozeb, Feb 9, 2010 IP
  2. mariush1007

    mariush1007 Member

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    hi,
    try to replace this line:

    $userbday = $varmonth . "//" . $varday . "//" . $varyear;
    Code (markup):
    with this line:

    $userbday = $varmonth . "/" . $varday . "/" . $varyear;
    Code (markup):
     
    mariush1007, Feb 9, 2010 IP
  3. blacksheep666

    blacksheep666 Active Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #3
    
    <?php
    	$varday = "12";
    	$varmonth = "2";
    	$varyear = "1996";
    	
    	$currentdata = getdate();
    	$userbday = $varmonth . "/" . $varday . "/" . $varyear;
    	
    	$age = $currentdata['year'] - $varyear;
    	if ($currentdata['mon'] < $varmonth)
    		$age--;
    
    	if ($currentdata['mon'] == $varmonth)
    		if ($currentdata['mday'] < $varday)
    			$age--;
    	
    	
    	if ($age < 13)
    		echo "[$age] You are under thirteen years old and therefore are not old enough to access this site!";
    	else
    		echo "[$age] old enough";		
    ?>
    
    
    PHP:
     
    blacksheep666, Feb 10, 2010 IP