brem13 Posted May 17, 2010 Share Posted May 17, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/202055-age-calculation/ Share on other sites More sharing options...
MadTechie Posted May 17, 2010 Share Posted May 17, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/202055-age-calculation/#findComment-1059584 Share on other sites More sharing options...
brem13 Posted May 17, 2010 Author Share Posted May 17, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/202055-age-calculation/#findComment-1059585 Share on other sites More sharing options...
MadTechie Posted May 17, 2010 Share Posted May 17, 2010 What do you want it to display ? for birthday echo $birthday; etc Quote Link to comment https://forums.phpfreaks.com/topic/202055-age-calculation/#findComment-1059587 Share on other sites More sharing options...
brem13 Posted May 17, 2010 Author Share Posted May 17, 2010 ok, i got it, i put the echo $year_diff; above the return $year_diff; and it worked... how could moving those make it work Quote Link to comment https://forums.phpfreaks.com/topic/202055-age-calculation/#findComment-1059602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.