Page 1 of 1

how to display the age?

Posted: Thu Sep 06, 2007 9:01 am
by noob#10
hey guys i have a code here that should display the age of the client base on the birthday he input in the database.

here is the code:

Code: Select all

<? 

require_once 'library/config.php';


if(isset($_SESSION['login_user']) && $_SESSION['login_user'] == 'ok'){

$user = $_SESSION['login_name'];

$sql = "SELECT * FROM tbl_customer WHERE Username = '$user'";
$result = dbQuery($sql);

$row = dbFetchAssoc($result); 
	extract($row);
	
$now = mktime();
$timeofbirth = mktime(0,0,0,$BDay);
$Age = $now - $timeofbirth;
$Age2 = $Age /(60*60*24*365.25);
?>
<table  cellpadding="0" cellspacing="0" border="1" width="300" style="font-family:sans-serif" align="center">
					<tr>
						<td>
							<?php require_once 'include/top.php';?>
						</td>
					</tr>
					<tr>
						<td>
							<?php require_once 'include/mainmenu.php';?>
						</td>
					</tr>
</table>
<form action="modifyprofile.php" method="post" name="modifyprofile">
<table width="48%"  cellpadding="5" cellspacing="0" border="1" style="font-family:sans-serif" align="center">
					<tr>
						<td align="center" colspan="2">MY ACCOUNT PROFILE</td>
					</tr>
					<tr>
						<td align="center">First Name</td>
						<td class="accountmenu"><input type="text" name="fname" value="<?php echo $FirstName;?>"></td>
					</tr>
					<tr>
						<td align="center">Last Name</td>
						<td class="accountmenu"><input type="text" name="lname" value="<?php echo $LastName;?>"></td>
					</tr>
					
					<tr>
						<td align="center">Birthday</td>
						<td class="accountmenu"><input type="text" name="bday" value="<?php echo $BDay;?>"></td>
					</tr>
					<tr>
						<td align="center">Age</td>
						<td class="accountmenu"><input type="text" name="age" value="<?php echo $Age2;?>"></td>
					</tr>
					<tr>
						<td align="center">Contact No.</td>
						<td class="accountmenu"><input type="text" name="contact" value="<?php echo $Contactno;?>"></td>
					</tr>
					<tr>
						<td align="center">Email</td>
						<td class="accountmenu"><input type="text" name="email" value="<?php echo $Email;?>"></td>
					</tr>
					<tr>
						<td align="center">Username</td>
						<td class="accountmenu"><input type="text" name="user" value="<?php echo $Username;?>"></td>
					</tr>
					<tr>
						<td class="accountmenu" colspan="2" align="center"><input type="submit" id="modifyprofile" value="modify">
						<input type="button" id="modifyprofile" value="back" onClick="window.location.href='index.php';"></td>
					</tr>
</table>
</form>
<?php
}
?>
</body>
it displays this error:

Notice: A non well formed numeric value encountered in C:\Program Files\xampp\xampp\htdocs\ieccTESTproj\viewprofile.php on line 17

i'm still testing on localhost..

and the age it displayed is this:
37.6798795853

the birthdate is:
1987-12-08

someone please help!

Posted: Thu Sep 06, 2007 9:29 am
by s.dot
I've found strtotime/mktime to be very unreliable (often off minutes or hours)

Using the month, date, and day in date() format is reliable.

Code: Select all

function getAge($year, $month, $day)
{
	   $tmonth = date('n');
	   $tday = date('j');
	   $tyear = date('Y');
	   $years = $tyear - $year;
	   if ($tmonth <= $month){
	       if ($month == $tmonth){
	           if ($day > $tday)
	               $years--;
	       }
	       else
	           $years--;
	   }
	   return $years;
}

Posted: Thu Sep 06, 2007 9:42 am
by Begby
You can also do this in MySQL if you have mysql 5.x

Code: Select all

SELECT *, TIMESTAMPDIFF(YEAR, Bday, CURDATE()) AS age
FROM tbl_customer WHERE Username = '$user'
It is also possible in mysql 4.x but its a pain and the code that scottavy posted is simpler.

Posted: Thu Sep 06, 2007 10:05 am
by noob#10
sir scotayy should i replace:

Code: Select all

$now = mktime();
$timeofbirth = mktime(0,0,0,$BDay);
$Age = $now - $timeofbirth;
$Age2 = $Age /(60*60*24*365.25);
with the one you gave?
one question sir how could the function call the date on the function?

Posted: Thu Sep 06, 2007 10:07 am
by noob#10
thanks sir Begby! i should try this too! thanks again!