Author Topic: [SOLVED] Time Problem  (Read 526 times)

0 Members and 1 Guest are viewing this topic.

Offline GrimlochTopic starter

  • Enthusiast
  • Posts: 66
  • Gender: Male
    • View Profile
    • Whisperwillow - PHP-Fusion Mod Support
[SOLVED] Time Problem
« on: April 05, 2009, 12:01:41 PM »
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:
Code: [Select]
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."&nbsp;";
echo "<a href='".BASEDIR."profile.php?lookup=".$db['user_id']."' title='".$db['user_name']."'>".$db['user_name']."&nbsp;</a><br />will be ";
echo "(<b>".$thistimee."</b> yrs old)&nbsp;".$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.

 8)
« Last Edit: April 05, 2009, 12:02:51 PM by Grimloch »
PHP-Fusion CMS Support Website
My Mods and Infusions
www.whisperwillow.com

Offline PFMaBiSmAd

  • Guru
  • 'Mind Boggling!'
  • *
  • Posts: 15,191
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Time Problem
« Reply #1 on: April 05, 2009, 12:09:09 PM »
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 -

Code: [Select]
$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 />";
« Last Edit: April 05, 2009, 12:10:19 PM by PFMaBiSmAd »
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline GrimlochTopic starter

  • Enthusiast
  • Posts: 66
  • Gender: Male
    • View Profile
    • Whisperwillow - PHP-Fusion Mod Support
Re: Time Problem
« Reply #2 on: April 05, 2009, 10:34:50 PM »
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:

Code: [Select]
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."&nbsp;";
echo "<a href='".BASEDIR."profile.php?lookup=".$db['user_id']."' title='".$db['user_name']."'>".$db['user_name']."&nbsp;</a><br />will be ";
echo "(<b>".$age."</b>) yrs old&nbsp;".$present."<br /><br />";

}

Thanks again and I will now mark this as solved.

 ;D
PHP-Fusion CMS Support Website
My Mods and Infusions
www.whisperwillow.com