convert birhtdate to age php
Posted: Thu Aug 23, 2007 11:56 pm
helo guys! i need help in my code here.. i've already posted the birth date but in addition i want to display the age which depends on the birth date that has been selected.. here is my form:
and here is my code:
i know this is easy but i get 0 years in my output. i think my computation is correct (maybe)..
please i need help..
Code: Select all
<?php
$years = "<select name=birthyear size=1>\n<option value=year>Select...\n";
for ($i = 1999; $i >= 1900; $i = $i - 1)
{
$years = $years . "<option>$i\n";
}
$years = $years . "</select>\n";
$months = "<select name=birthmonth size=1>\n<option value=month>Select...\n";
for ($i = 1; $i <= 12; $i++) {
$monthname = date("F", mktime(12, 0, 0, $i, 1, 2000));
$months = $months . "<option value=$i>$monthname\n";
}
$months = $months . "</select>\n";
# then the days
$days = "<select name=birthday size=1>\n<option value=day>Select...\n";
for ($i = 1; $i <= 31; $i++) {
$days = $days . "<option>$i\n";
}
$days = $days . "</select>\n";
# finally years ... 1900-1999 for now
?>
<form action="test1.php" method="post" name="frmAddUser" id="frmAddUser" onsubmit='return formValidator()' >
<b>Year</b>
<?php echo $years; ?>
<b>Month</b>
<?php echo $months; ?>
<b>Day</b>
<?php echo $days;?>
<input type="hidden" name='BDay' value= "<?php echo $BDay; ?>" />
<input name="btnAddUser" type="submit" value="Register" onClick="return checkPassword();" />Code: Select all
<?php
$year = $_POST['birthyear'];
$month = $_POST['birthmonth'];
$day = $_POST['birthday'];
$now = mktime();
$timeofbirth = mktime(0,0,0,$birthmonth,$birthday,$birthyear);
$Age = $now - $timeofbirth;
echo $BDay = $year."-".$month."-".$day;
echo "<b>". floor($Age/(60*60*24*365.25)) ."</b> years.\n<p>\n";
?>please i need help..