joshuadavid2007
10-14-2007, 02:57 PM
function age($month, $day, $year)
{
(checkdate($month, $day, $year)==0) ? die("no such date.") : "";
$y=gmstrftime("%Y");
$m=gmstrftime("%m");
$d=gmstrftime("%d");
$age=$y-$year;
return (($m<=$month)&&($d<=$day)) ? $age-1 : $age;
}
the code above takes month, date, and year of birth as arguments, check if they exist/are valid, if yes it continues, take current year, month, and date...does some calculations and reasoning, and returns the age...
CFMaBiSmAd
10-14-2007, 03:27 PM
If you were to edit that post so that it is formatted so that it is readable, it would help. There is also no reason for the [ code][ /code] tags in addition to the [ php][ /php] tags, that just makes it more unreadable.
Zangeel
11-15-2008, 05:10 PM
<?php
function age($month, $day, $year)
{
(checkdate($month, $day, $year) == 0) ? die("no such date.") : "";
$y = gmstrftime("%Y");
$m = gmstrftime("%m");
$d = gmstrftime("%d");
$age = $y - $year;
return (($m <= $month) && ($d <= $day)) ? $age - 1 : $age;
}
?>
<?php
function age($month, $day, $year)
{
(checkdate($month, $day, $year) == 0) ? die("no such date.") : "";
$y = gmstrftime("%Y");
$m = gmstrftime("%m");
$d = gmstrftime("%d");
$age = $y - $year;
return (($m <= $month) && ($d <= $day)) ? $age - 1 : $age;
}
?>
Didn't quite work... wasn't detecting the right age. Here is my modified code that seems to be working fine:
function age($month, $day, $year)
{
(checkdate($month, $day, $year) == 0) ? die("no such date.") : "";
$y = gmstrftime("%Y");
$m = gmstrftime("%m");
$d = gmstrftime("%d");
$age = $y - $year;
if($m <= $month)
{
if($m == $month)
{
if($d < $day) $age = $age - 1;
}
else $age = $age - 1;
}
return($age);
}
kbluhm
04-13-2009, 05:39 PM
I wouldn't recommend hijacking the output from within the function using exit(), die(), etc... Just grab the data and control the output from the `view` scope:
function age( $month, $day, $year )
{
if ( checkdate( $month, $day, $year ) )
{
//calculate $age as an integer
return age;
}
return FALSE;
}
Example:
$age = age( 12, 25, 1990 );
if ( FALSE === $age )
{
echo 'Not a valid birth date';
}
else
{
echo 'Your age: ' . $age;
}