sikkhost
11-20-2005, 02:20 AM
Hey Guys,
I need a simple way to verify that someone is 13 years old. It needs to be done through PHP and the age format was made by date() and looks like this:
"March 5, 1990"
Anyone know how to do this?
Much thanks for your help in advance!
-Chris
hiryuu
11-20-2005, 06:34 AM
You probably want to use mktime to get the timestamp for 13 years ago. Then use strtotime to break the date you have into a timestamp. Then compare. There are several examples in the user comments in the PHP manual.
malenski
11-20-2005, 11:07 AM
<?php
$date = "March 5, 1990";
function check_age($age){
return (strtotime('13 years ago') > strtotime($age));
}
$results = (check_age($date))? 'Yes':'No';
echo "Birthdate: $date\n";
echo "Are you 13? $results\n";
?>
sikkhost
11-20-2005, 02:48 PM
Works Perfectly! Thank you very much malenski!
-Chris
PHPCoder0151
11-22-2005, 02:22 AM
The easier way!!
<?php
$age = date("Y") - $_POST['year'];
if ($age < 13) {
die("WRONG AGE DUMMIE");
}
?>
hiryuu
11-22-2005, 05:56 AM
It's also the wrong way -- depending on when they were born within the year, they could only be 12 (and some change) and still pass.
strtotime() is a really fun function.
Tree NC
11-22-2005, 10:20 AM
Using die() also isn't a very good way of posting messages. There may be more code that needs to run, such as including a footer or something. Malenski's code is the best way to go about it.
ubersmith_boo
11-22-2005, 04:31 PM
strtotime() is a cool function but it worries me to rely on a function that will supposedly decode english into a number. For this i'd likely just use 409968000 as the number of seconds in 13 years. This is just a personal preference though.
malenski
11-22-2005, 05:23 PM
The number of seconds in 13 years changes depending on the years.
I agree you should validate the format of the string you send to strtotime before using it.
klown
11-22-2005, 09:36 PM
Yeah I do agree using die() isnt a good idea.
PHPCoder0151
11-23-2005, 11:30 AM
Lol I dont use die ;) I just used it in the example.
I have error() and notice() functions :D:D