Jump to content

[SOLVED] Time Problem


Grimloch

Recommended Posts

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.

 

8)

Link to comment
Share on other sites

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 />";

Link to comment
Share on other sites

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.

 

;D

Link to comment
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.