...

View Full Version : PHP Age Script



Skyzyx
01-16-2004, 07: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:

Nightfire
01-16-2004, 07: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 :)

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


<?php

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">
</form>

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

x_goose_x
01-18-2004, 11:36 PM
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.

ReadMe.txt
01-18-2004, 11:58 PM
just a v minor perfomance note, you may want to replace 60*60*24*365.25 with 31471200.

x_goose_x
01-19-2004, 03: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:

http://www.google.ca/search?q=days+in+a+year&ie=ISO-8859-1&hl=en&meta=



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum