convert birhtdate to age php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
noob#10
Forum Newbie
Posts: 19
Joined: Wed Aug 15, 2007 9:55 am

convert birhtdate to age php

Post by noob#10 »

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:

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();" />
and here is my code:

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";

 ?>
i know this is easy but i get 0 years in my output. i think my computation is correct (maybe)..

please i need help..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

If you print $now, $timeofbirth and $Age to see what values they have.
(#10850)
User avatar
noob#10
Forum Newbie
Posts: 19
Joined: Wed Aug 15, 2007 9:55 am

Post by noob#10 »

sir i don't know what you mean.. should i echo the values of $timeofbirth,$now,$Age?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Yes, that would have helped you find the problem. But looking at your code again, I think the problem is that you don't set $birthmonth, $birthday or $birthyear anywhere.
(#10850)
Post Reply