Grimloch Posted April 5, 2009 Share Posted April 5, 2009 Hello all, It's been a while since I've been on here. I have a coding problem. The system is PHP-Fusion ver7.00.05. Another user created a birthday display panel and there are some time calculation errors in his logic that I can't seem to figure out. Its supposed to display users with birthdays in the current month. OK... I have set some users birthdays to the current month just for testing purposes with varying years of birth. Some display fine but some show a minus value for age. Here is the code: while ($db = dbarray($result)) { $date = explode ("-", $db['user_birthdate']); $now = date("Y"); if ($date[2] == date("d")) { $present = "<img src='".INFUSIONS."upcoming_birthday_panel/images/birthday.gif' alt='' border='0' />\n"; } else { $present = ""; } $thistime = time() - mktime(0,0,0,$date['1'],$date['2'],$date['0']); $thistimee = date("Y",$thistime) -1970; if ($date[2] > date("d")) { $thistimee = $thistimee + 1; } else { $thistimee = $thistimee; } echo "On ".$date[1]."/".$date[2]."/".$now." "; echo "<a href='".BASEDIR."profile.php?lookup=".$db['user_id']."' title='".$db['user_name']."'>".$db['user_name']." </a><br />will be "; echo "(<b>".$thistimee."</b> yrs old) ".$present."<br /><br />"; } The users birthdate is stored in the database as: (Y-M-D) i.e. 1949-11-03 would be my birthdate. I changed mine to 1949-04-05 and it shows correctly that I will be 60 yrs old on today. I changed another user to 1958-04-27 and it correctly shows that he will be 51 on the 27th of April. Another user I changed to 1929-04-17 and the display shows that he will be -56 yrs old on the 17th of April. I do not understand why the other coder is using the value of [-1970] in his calculation and I think that is part of the source of the problem. Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 5, 2009 Share Posted April 5, 2009 The definition of a persons age is the difference between the current year and the year of birth, subtract one if the birthday has not occurred in the current year. Here is an example of doing this in a query - http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html The php code to do this is equally simple - $date = "1929-04-17"; $today = date("Y-m-d"); $age = substr($today, 0, 4) - substr($date, 0, 4) - (substr($today, 5,5) < substr($date, 5,5)); echo "DOB: $date, Age: $age<br />"; Quote Link to comment Share on other sites More sharing options...
Grimloch Posted April 6, 2009 Author Share Posted April 6, 2009 Hey PFMaBiSmAd; I appreciate your quick reply. Using ideas from your sample code I was able to get the script to work completely as I wanted. Here is the final code that worked correctly for me: while ($db = dbarray($result)) { $newdate = explode ("-", $db['user_birthdate']); //My New Code $date = $newdate; $now = date("Y-m-d"); $yr = date("Y"); $age = substr($now, 0, 4) - substr($db['user_birthdate'], 0, 4); //End new code if ($date[2] == date("d")) { $present = "<img src='".INFUSIONS."upcoming_birthday_panel/images/birthday.gif' alt='' border='0' />\n"; } else { $present = ""; } $thistime = time() - mktime(0,0,0,$date['1'],$date['2'],$date['0']); echo $date[1]."/".$date[2]."/".$yr." "; echo "<a href='".BASEDIR."profile.php?lookup=".$db['user_id']."' title='".$db['user_name']."'>".$db['user_name']." </a><br />will be "; echo "(<b>".$age."</b>) yrs old ".$present."<br /><br />"; } Thanks again and I will now mark this as solved. Quote Link to comment 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.