View Full Version : PHP Age Script

01-16-2004, 08:37 PM
I'm moving along in my learning of PHP. This is the next step.

I'm looking for a script where I can pass my birthday, birthmonth, and birthyear as parameters and have it either return or echo my current age.

I'm sure this will have something to do with date() and/or mktime(), but after Googling for a bit, I haven't found anything that I can really work with.

I could write one in JavaScript, but I'm looking for a server-side solution. I know that in JavaScript, you need to account for leap years. I'm not sure if that would apply in PHP, but I figured I'd throw that out there to anyone who'd be willing to write me something.

Any help is appreciated! :thumbsup:

01-16-2004, 08:46 PM
:) It's not that hard at all
I not got time to show you how to do it, but there a few a few examples at http://uk.php.net/date :)

01-18-2004, 07:16 PM
Just reinstalled windows and my web server isn't up yet, so I can't test it, but this should work:


if ( isset ( $HTTP_GET_VARS['d'] ) && isset ( $HTTP_GET_VARS['m'] ) && isset ( $HTTP_GET_VARS['y'] ) ) {
$d = isset ( $HTTP_GET_VARS['d'] );
$m = isset ( $HTTP_GET_VARS['m'] );
$y = isset ( $HTTP_GET_VARS['y'] );
$bDay = mktime ( 0, 0, 0, $m, $d, $y );
$cTime = time ();
$age = $cTime-$bDay;
$yr = floor ( $age/(60*60*24*356.25) );
print "You are $yr years old";

<form method="GET" action="">
Day: <input type="text" name="d"><br>
Month: <input type="text" name="m"><br>
Year (ex. 1972): <input type="text" name="y"><br>
<input type="submit" value="Calculate">

01-18-2004, 09:07 PM
Thank you. I was having a heck of a time looking through the documentation for the PHP functions. :thumbsup:

01-19-2004, 12:36 AM
A little subnote, the variable $age is the persons age in seconds, that's why I did all those divisions. You could also do some more calculations to find how many months, days, ... old they are.

01-19-2004, 12:58 AM
just a v minor perfomance note, you may want to replace 60*60*24*365.25 with 31471200.

01-19-2004, 04:29 AM
Ya, I made a typo, sorry.

The reason I did it like that was so it is easier to understand.

In case you want exact figure: