How to Validate Age With PHP
Sunday, May 3rd, 2009
I was recently browsing the analytic statistics and information for Dev-Tips when I noticed that a common search was appearing on the site. The search terms were how to validate age with php. I immediately saw this as an opportunity to provide an answer to a very simple php question, as it is quite simple to validate an age with php. Let's have a quick look and write a script that will allow us to validate that a user is a certain age with php.
First, let me say that there are a lot of different ways that we could do this, we could spend weeks writing different ways on how to accomplish this best. Disclaimer aside, let's look at a simple and procedural option.
<?php
//users birthday, ideally you would take this data from a form
$user_bd = strtotime('March 14, 1997');
//Age requirement, using 21 as the minimum age required
$age_req = strtotime('+21 years', $user_bd);
//Grab the current UNIX timestamp and compare it to the minimum required
if(time() < $age_req){
echo 'Not 21 years or older.';
}
else{
echo 'Word. Come on in.';
}
?>
Our script goes through the following steps:
- Parse the users birthday as a UNIX timestamp using strtotime() and store the result in a variable. Ideally this info would come from a form/select box on your page.
- Determine the minimum age they need to be. We will use 21 years old. We add 21 years to our original date, parse it as a UNIX timestamp, and store it in a variable.
- If the current timestamp is less than the age required, print to the screen or do whatever you wish with the user (re-direct, exit() etc.). Else, the user is old enough.
And that is one way you can validate any age with php. Hopefully, the users coming to this site will now find it more helpful that we have posted the answer to the common search query. Happy coding!
If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!
Article Sponsored by:
The Ultimate Image Gallery Manager was created by Drew Douglass, the site editor here at Dev-Tips. The Ultimate Gallery is installed directly on your server, just like WordPress or a CMS. It quickly and easily allows you to manange thousands of image galleries and images from it's easy to use backend admin panel. It's also packed full of photo editing features. Available exclusively on ThemeForest.









Drew
thanks for this. Always on the lookout for cool and useful PHP tricks and scripts.
mary
You’re very welcome Mary, thanks for your support
I never noticed the second parameter for strtotime! Thanks for this Drew!
I forgot about it too until I was looking at it in the manual again and realized this was going to be super easy
Thanks for your nice words!
This got me thinking… what am I still doing with date_diff haha.
Thanks for the article Drew =)
Thanks for your comment Gaya
I completely forgot about date_diff and its’ aliases. I am sure that would work just as well.
Thanks for the tip, I wasn’t aware of the strtotime() function
this is very useful thanks for the post! keep on coming…