0

I've looked at many posts on here and I still cant figure this out.

I am trying to verify that someone is older than 13 before they register for my site. This is what i have so far

<?php
if (is_string($_POST['birthday']) && $_POST['birthday'] != 'mm/dd/yyyy')
{
    $dateObj = new DateTime($_POST['birthday']);
    $ageLimit = new DateTime('13 years');
    $now = new DateTime(date("Y/m/d"));
    $checkDate = $now->diff($ageLimit);;
    if($checkDate > $dateObj)
    {
        $errors[]='You must be atleast 13 years old to join.';
    }
    else
    {
        $bday = mysqli_real_escape_string($db,$_POST['birthday']);
    }
}
else
{
    $errors[]= 'Enter your birthday.';
}

The code will always run to

$bday = mysqli_real_escape_string($db,$_POST['birthday']);}

no matter what is entered in the date field and the outcome is always 1.

Can anyone help me with this? I cant figure this one out on my own.

<b>Birth Date</b><br><input type="date" name="birthday"
value=<?php if(isset($_POST['birthday']))echo $_POST['birthday'];?>><br>

3 Answers3

1

Comparaison operators work with DateTime, see the answer here. So something like this should work

$dateObj=new DateTime($_POST['birthday']);
$ageLimit=new DateTime('-13 years');
if($dateObj > $ageLimit){
   //TOO YOUNG
}

EDIT per comment

Replace if(isset($_POST['birthday']))echo $_POST['birthday']; with

if(isset($_POST['birthday'])) { 
  echo $_POST['birthday']; 
} else { 
  echo 'mm/dd/yyyy';
}

Or change

if (is_string($_POST['birthday']) && $_POST['birthday'] != 'mm/dd/yyyy')

To

  if (!empty($_POST['birthday']) && is_string($_POST['birthday']))
Community
  • 1
  • 1
Tom Tom
  • 3,680
  • 5
  • 35
  • 40
1

You have several errors

  1. '13 years' is not a valid value for DateTime()
  2. A date in the 'Y/m/d' format is not a valid format for DateTime()
  3. $checkDate is a DateInterval object and is not comparable to a DateTime object

You can fix this and simplify your code by comparing DateTime objects which are comparable:

$birthday = new DateTime($_POST['birthday']);
$ageLimit = new DateTime('-13 years');
if ($birthday < $ageLimit) {
    // they're old enough
}
else {
     // too young
} 

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • cool man thanks. only one thing though. I can not see if they have entered a date or not. i would like to give them a message saying please enter the date. but it appears as a age younger than 13 when no birthday is inputed and the string is mm/dd/yyyy – Travis Hallet Feb 21 '15 at 01:23
0

It might be easier to use strtotime to calculate the date difference. The higher the number the younger somebody is. So if the persons age is higher than the minimal age number they are not old enough.

if(is_string($_POST['birthday'])&&$_POST['birthday']!='mm/dd/yyyy') {
   $minAge = strtotime("-13 years");
   $dateObject = strtotime($_POST['birthday']);

   if($dateObject > $minAge) {
      $errors[]= 'You must be atleast 13 years old to join.';
   }

} else {
    $errors[]='Enter your birthday.';
}
Bas Slats
  • 277
  • 2
  • 10