Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.