Accurax Posted March 2, 2007 Share Posted March 2, 2007 Im trying to pull a date of birth from my database (mysql) and use this to calculate a users age for display on the page. At the moment i'm storing the date of birth of each user in three different INT fields in my table Year - INT - 4 Day - INT - 2 Month - INT - 2 here is the code im trying to use to calculate the age: <?php //***** $username = $_SESSION['username']; $query = "SELECT * FROM users WHERE user_name='$username'"; $result = mysql_query($query) or die ("could not find name"); $row = mysql_fetch_array($result); $day = $row['day']; $month = $row['month']; $year = $row['year']; $birthday = $day-$month-$year; $today = date('d-m-Y'); $a_birthday = explode('-', $birthday); $a_today = explode('-', $today); $day_birthday = $a_birthday[0]; $month_birthday = $a_birthday[1]; $year_birthday = $a_birthday[2]; $day_today = $a_today[0]; $month_today = $a_today[1]; $year_today = $a_today[2]; $age = $year_today - $year_birthday; if (($month_today < $month_birthday) || ($month_today == $month_birthday && $day_today < $day_birthday)) { $age--; } echo $age; ?> Can anyone please make any suggestions as to why this doesnt work? I dont get an error message at all, it just displays the age as around 2007 all the time. All help appreciated on this, thanks Link to comment https://forums.phpfreaks.com/topic/40857-problem-calculating-age/ Share on other sites More sharing options...
itsmeArry Posted March 2, 2007 Share Posted March 2, 2007 here is the problem instead of this $birthday = $day-$month-$year; use this $birthday = $day."-".$month."-".$year; Link to comment https://forums.phpfreaks.com/topic/40857-problem-calculating-age/#findComment-197813 Share on other sites More sharing options...
Accurax Posted March 2, 2007 Author Share Posted March 2, 2007 I figured it was that line .... but i still have some problems with syntax. Thankyou so much Link to comment https://forums.phpfreaks.com/topic/40857-problem-calculating-age/#findComment-197817 Share on other sites More sharing options...
itsmeArry Posted March 2, 2007 Share Posted March 2, 2007 what is the error... Link to comment https://forums.phpfreaks.com/topic/40857-problem-calculating-age/#findComment-197819 Share on other sites More sharing options...
craygo Posted March 2, 2007 Share Posted March 2, 2007 Can use this <?php <?php function your_age($month, $day, $year){ $cday = date("d"); $cmonth = date("m"); $cyear = date("Y"); $today = gregoriantojd($cmonth, $cday, $cyear); $birthday = gregoriantojd($month, $day, $year); $age = $today-$birthday; $years = floor($age/365.25); $days = fmod($age,365.25); return "You are $years years and $days days old"; } echo your_age("12", "24", "1945")."<br />"; ?> Ray Link to comment https://forums.phpfreaks.com/topic/40857-problem-calculating-age/#findComment-197820 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.