how to display the age?

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

how to display the age?

Post 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!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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;
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
User avatar
noob#10
Forum Newbie
Posts: 19
Joined: Wed Aug 15, 2007 9:55 am

Post 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?
User avatar
noob#10
Forum Newbie
Posts: 19
Joined: Wed Aug 15, 2007 9:55 am

Post by noob#10 »

thanks sir Begby! i should try this too! thanks again!
Post Reply