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 Answers 3

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'])) 
Sign up to request clarification or add additional context in comments.

5 Comments

Shouldn't this be if($dateObj < $ageLimit)
Nope because if the birthday is more recent that 13 years ago it means the user is too young.
You're correct, I'm just confused sorry =) Perhaps it's time to go to bed.
yea i figured that part but i want to output a message if they have not entered one. i mean the code is good how it is now. i can work with it but i would like it to be more accurate
it still goes through and makes it a variable able to be posted. IDK what is wrong if i set it null instead of mm/dd/yyyy and do the check will it work?
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

1 Comment

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
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.'; } 

1 Comment

cool this is working but i still cant get it to output "Enter your birthday" when the date is not chosen

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.