how to display the age?

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:


<? 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); ?>
<?php require_once 'include/top.php';?>
<?php require_once 'include/mainmenu.php';?>
					<td align="center">Birthday</td>
					<td class="accountmenu"><input type="text" name="bday" value="<?php echo $BDay;?>"></td>
					<td align="center">Age</td>
					<td class="accountmenu"><input type="text" name="age" value="<?php echo $Age2;?>"></td>
					<td align="center">Contact No.</td>
					<td class="accountmenu"><input type="text" name="contact" value="<?php echo $Contactno;?>"></td>
					<td align="center">Email</td>
					<td class="accountmenu"><input type="text" name="email" value="<?php echo $Email;?>"></td>
					<td align="center">Username</td>
					<td class="accountmenu"><input type="text" name="user" value="<?php echo $Username;?>"></td>
					<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>
First Name
Last Name
<?php } ?>


it displays this error:

Notice: A non well formed numeric value encountered in C:Program FilesxamppxampphtdocsieccTESTprojviewprofile.php on line 17

i’m still testing on localhost…

and the age it displayed is this:

the birthdate is:

someone please help!

please suround ur code with and .
in its just php (no html) with [php] and [/php].

is that better sir?

line 17:
[php]$timeofbirth = mktime(0,0,0,$BDay);[/php]
read and u will see the 4th parameter of mktime is month, and that its an integer.

if u try to use “1987-12-08” as a number u get this notice: “A non well formed numeric value encountered”

So basically what Q1712 is saying is you need to convert yyyy-mm-dd to mm-dd-yyyy in order for this to work.

There are a couple of ways to do this. The easiest way I found when pulling from a database, MySQL is DATE_FORMAT(mydate, %m-%d-%Y)

I am not exactly sure if that is the correct formatting of that, but if you go to … ate-format

This should give you all the information you need.

If you are using MSSQL you will need to look into DATEPART() as I am unsure if there is a simple date formatting function. Only have had minimal exposure to MSSQL.

Hope this helps.

no, what i’m trying to say is that mktime will never take a str-date as argument. no metter whether its yyyy-mm-dd or mm-dd-yyyy.

take a look at:

and try to find ur way, and of cause ask again when u get stucked.

btw i wouldnt try to use a timestamp to calculate the age, but compare the year, month and day on there own, to get todays year, month and day u just need the date() function

Oooppss… Yes Q1712 is right. Sorry about that, I was thinking when I did something similar to this and was having a similar problem, that I just needed to change the date format. I remember now that wasn’t the problem.

how about:
$Age=date(‘Y’)-$y-1 +($m*31+$d <= date(‘m’)*31+date(‘d’));[/php]

$dob = ‘09-06-1997’;
$today = date(‘m-d-Y’);

$a_dob = explode(’-’, $dob);
$a_today = explode(’-’, $today);

$day_dob = $a_dob[1];
$month_dob = $a_dob[0];
$year_dob = $a_dob[2];
$day_today = $a_today[1];
$month_today = $a_today[0];
$year_today = $a_today[2];

$age = $year_today - $year_dob;

if (($month_today < $month_dob) || ($month_today == $month_dob && $day_today < $day_dob))
echo $age;

Here you should be able to modify this code to suit your needs was written in like 2 minutes. Is working, but just needs to be modified to fit what you need it for.

Sponsor our Newsletter | Privacy Policy | Terms of Service