Calculate age
Posted: Sat May 09, 2009 9:48 am
Hi there!
How do I calculate age for a user when I've got f.i. 02-21-87 in the mysql db (TEXT)?
How do I calculate age for a user when I've got f.i. 02-21-87 in the mysql db (TEXT)?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
function get_age($year, $month, $day) {
return ((date("d") - (int)$day) < 0 || (date("m") - (int)$month) < 0) ? (date("Y") - (int)$year) : (date("Y") - ((int)$year - 1));
}
Code: Select all
<?php
function doAge($year, $month, $day) {
return ((date("d") - (int)$day) < 0 || (date("m") - (int)$month) < 0) ? (date("Y") - (int)$year) : (date("Y") - ((int)$year - 1));
}
?>
<p><?php echo doAge($fetch['born_date']); ?></p>Code: Select all
Warning: Missing argument 2 for doAge(), called in /home/age.php on line 112 and defined in /home/age.php on line 108
Warning: Missing argument 3 for doAge(), called in /home/age.php on line 112 and defined in /home/age.php on line 108Code: Select all
$dob = explode('/',$fetch);//fetch = 09/20/1987Code: Select all
echo $dob[0]; //09
echo $dob[1]; //20
echo $dob[2]; //197Code: Select all
doAge($dob[0], $dob[1], $dob[2]);Code: Select all
<?php
function doAge($year, $month, $day) {
return ((date("d") - (int)$day) < 0 || (date("m") - (int)$month) < 0) ? (date("Y") - (int)$year) : (date("Y") - ((int)$year - 1));
}
$dob = explode('.',$fetch['born_date']);
echo $dob[0].'<br />';
echo $dob[1].'<br />';
echo $dob[2].'<br /><br />';
echo doAge($dob[0], $dob[1], $dob[2]);
?>Code: Select all
function doAge($year, $month, $day) {
return ((date('m') - (int)$month <= 0) && (date("d") - (int)$day > 0)) ? date('Y') - (int)($year) - 1 : date('Y') - (int)($year);
}
Code: Select all
function doAge($year, $month, $day) {
# if this month minus month of birth is greater than or equal to 0
if (date('m') - (int)$month >= 0) {
# if this date minus the date of birth is greater than or equal to 0
if (date("d") - (int)$day >= 0) {
return date('Y') - (int)($year);
}
}
return date('Y') - (int)($year) - 1;
}
function doAge2($year, $month, $day) {
return ((date('m') - (int)$month >= 0) && (date("d") - (int)$day >= 0)) ? date('Y') - (int)($year) : date('Y') - (int)($year) - 1;
}