Calculating Age
Posted: Fri Aug 14, 2009 3:41 pm
Hi All,
I have produced a function to calculate the age of an input from a date in the format YYYYMMDD.
What I want to achieve is that there is a drop down list that sets the values for the input date (currently static as for testing but will be dynamic when I complete the page.)
the selection box is named chgala and has an onchange command which changes the selection. The rest of the code is shown here:
Everything that I can see has a description by it to show what it is doing.
Following this I have the table layout to display the data and then a while statement to print the data in the table. within this there is the function caption that calculates the age from a date of birth (in the format YYYYMMDD)
and then when I want to display the age I just echo or print '$Age'
This seems to work if I take out the date function at the start to change the date and have it work off a current date, but now with this changing of date no matter which I select it seems to calculate their and and then minus 2009 (current year).
i.e. if someone should be 4 years old today then this will show as -2005.
Can anyone see where this is going wrong, why it is taking off the current year and why it doesnt change when I change the selection.
If anyone can see where I am going wrong and correct me I will be very grateful or if someone can offer an alternative for me yo use that would also be appreciated.
Thanks in advance
I have produced a function to calculate the age of an input from a date in the format YYYYMMDD.
What I want to achieve is that there is a drop down list that sets the values for the input date (currently static as for testing but will be dynamic when I complete the page.)
the selection box is named chgala and has an onchange command which changes the selection. The rest of the code is shown here:
Code: Select all
if (isset($_POST['chgala'])) { $selectedyear = "$_POST[chgala]"; } else { $selectedyear = date("Y")."1231"; }
include 'dbc.php';
$sql="SELECT * FROM membership WHERE member = 'Y' ORDER BY DOB DESC";
$result = mysql_query($sql);
$GalaDate = $selectedyear;
$gdy = substr($GalaDate,0,4);
$gdm = substr($GalaDate,4,2);
$gdd = substr($GalaDate,6,2);
echo "gdy = ".$gdy." gdm = ".$gdm." gdd = ".$gdd;
function DetermineAgeFromDOB ($YYYYMMDD_In)
{
// Parse Birthday Input Into Local Variables
// Assumes Input In Form: YYYYMMDD
$yIn=substr($YYYYMMDD_In, 0, 4);
$mIn=substr($YYYYMMDD_In, 4, 2);
$dIn=substr($YYYYMMDD_In, 6, 2);
// Calculate Differences Between Birthday And Date Set
// By Subtracting Birthday From Date Set
$ddiff = $gdd - $dIn;
$mdiff = $gdm - $mIn;
$ydiff = $gdy - $yIn;
// Check If Birthday Month Has Been Reached
if ($mdiff < 0)
{
// Birthday Month Not Reached
// Subtract 1 Year From Age
$ydiff--;
} elseif ($mdiff==0)
{
// Birthday Month Currently
// Check If BirthdayDay Passed
if ($ddiff < 0)
{
//Birthday Not Reached
// Subtract 1 Year From Age
$ydiff--;
}
}
return $ydiff;
}
Following this I have the table layout to display the data and then a while statement to print the data in the table. within this there is the function caption that calculates the age from a date of birth (in the format YYYYMMDD)
Code: Select all
while($rows=mysql_fetch_array($result)){
// Example Age Input
$DateOfBirth= $rows['DOB'];
// Calculate Age Using Function:
$Age= DetermineAgeFromDOB ($DateOfBirth);
$doby=substr($DateOfBirth,0,4);
$dobm=substr($DateOfBirth,4,2);
$dobd=substr($DateOfBirth,6,2);
This seems to work if I take out the date function at the start to change the date and have it work off a current date, but now with this changing of date no matter which I select it seems to calculate their and and then minus 2009 (current year).
i.e. if someone should be 4 years old today then this will show as -2005.
Can anyone see where this is going wrong, why it is taking off the current year and why it doesnt change when I change the selection.
If anyone can see where I am going wrong and correct me I will be very grateful or if someone can offer an alternative for me yo use that would also be appreciated.
Thanks in advance