I'm trying to pass a date to a function that calculates the age of a person. However, the date is in Y-m-d format in the database, and I need to pass it in "Y-m-d" format. I have tried string concatenation, but that failed, maybe because it just operated the numbers using minus(-) operator. Kindly let me know how to do the same.
I am fetching the DOB in $dob variable, and passing it to CalculateAge($dateofbirth) function
Here is the code:
function CalculateAge($BirthDate)
{
// Put the year, month and day in separate variables
list($Year, $Month, $Day) = explode("-", $BirthDate);
//echo $Year;
$YearDiff = date("Y") - $Year;
// If the birthday hasn't arrived yet this year, the person is one year younger
if(date("m") < $Month || (date("m") == $Month && date("d") < $Day))
{
$YearDiff--;
}
if(date("m") > $Month || date("m") == $Month)
$MonthDiff = date("m") - $Month;
else
$MonthDiff = 12 - $Month + date("m");
$age = $YearDiff + $MonthDiff/12;
return $age;
}
$dob = mysql_query("SELECT date_of_birth FROM kids_informations WHERE user_id = '$usid'");
$rs = CalculateAge($dob);