Jump to content

age calculation


brem13

Recommended Posts

hey, i'm trying to calculate the age of people using birthdays from my database. however it's not showing any results.

i'm trying to take the year, month and day from a database, put it together in date format and calculate the age, but its not giving any results.

mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); 
$result = mysql_db_query($database, "select * from $table ") or die (mysql_error());
while($qry = mysql_fetch_array($result)){ 
	$year = $qry['year'];
	$month = $qry['month'];
	$day = $qry['day'];
	$birthday = $year."-".$month."-".$day;
}//end while
function birthday($birthday)
	{

    		list($birth_year,$birth_month,$birth_day) = explode("-",$birthday);
    		$year_diff  = date("Y") - $birth_year;
    		$month_diff = date("m") - $birth_month;
    		$day_diff   = date("d") - $birth_day;
    		if ($month_diff < 0) $year_diff--;
    		elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    		return $year_diff;
    		echo $year_diff;
     	}

 

Link to comment
https://forums.phpfreaks.com/topic/202055-age-calculation/
Share on other sites

shouldn't

$birthday = $year."-".$month."-".$day;

be

$birthday = birthday($year."-".$month."-".$day);

 

and

 

return $year_diff;
echo $year_diff;

be

echo $year_diff;
return $year_diff;

Infact not sure why you even have the echo!

 

and you maybe better off sending 3 parameters instead of 1

Link to comment
https://forums.phpfreaks.com/topic/202055-age-calculation/#findComment-1059584
Share on other sites

ok, i changed the birthday line and commented out the echo but it's still showing a blank

mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); 
$result = mysql_db_query($database, "select * from $table ") or die (mysql_error());
while($qry = mysql_fetch_array($result)){ 
	$year = $qry['year'];
	$month = $qry['month'];
	$day = $qry['day'];
	$birthday = birthday($year."-".$month."-".$day);
}//end while
function birthday($birthday)
	{

    		list($birth_year,$birth_month,$birth_day) = explode("-",$birthday);
    		$year_diff  = date("Y") - $birth_year;
    		$month_diff = date("m") - $birth_month;
    		$day_diff   = date("d") - $birth_day;
    		if ($month_diff < 0) $year_diff--;
    		elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    		return $year_diff;
    		//echo $year_diff;
     	}

 

Link to comment
https://forums.phpfreaks.com/topic/202055-age-calculation/#findComment-1059585
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.