Click to See Complete Forum and Search --> : [RESOLVED] Calculate the Age
dbasenoob
Aug 17th, 2010, 09:24 AM
How can i get my exact age? i make a code but it doesn't work. i want to happen is calculate the age. date now - date of birth then will show my age.
<?php
$day = $_POST['day']; //using select or drop down list
$month = $_POST['month']; //using select or drop down list
$year = $_POST['year']; //using select or drop down list
$age = date(("m-d-Y") - ($month.$day.$year));
echo "<h1>Your age now is $age </h1>";
?>
kows
Aug 17th, 2010, 10:51 AM
first, use mktime() to create a Unix timestamp.
$birthdate = mktime(0, 0, 0, (int) $_POST['month'], (int) $_POST['day'], (int) $_POST['year']);
then, subtract the years. if you're only interested in the year:
echo "you are " . (date('Y') - date('Y', $birthdate)) . " years old.";
alternatively, if you are not storing the $birthdate variable in a database or something, you could make it even more simple:
echo " you are " . (date('Y') - (int) $_POST['year']) . " years old.";
dbasenoob
Aug 21st, 2010, 04:30 AM
thanks it works... but how could i use the day and month? let say my birthday is august 25 1988 and my current age is 21. when august 25 past my age will be 22? how could i code it?
kows
Aug 22nd, 2010, 01:50 AM
the first solution in my post that makes use of mktime() will take the day and month of the year into account. the second method only deals with the year.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.