...

View Full Version : How do I change this code to ignore part of a date?



ClassicNancy
06-11-2007, 10:36 PM
I have this mod for my forum and it calls the birthdate from the database. I would like to have it not see the year. What is happening if a person puts a year in the box in their profile it makes it Dec 31st because that year is already over. There is no way to remove that box from the code according to the forum tech people. I've added the two pieces that refer to date. It already is written where it only shows the month and day in the view. Thank you to anyone that can help.



$birthdays = get_birthdays();
if(!$birthdays) {
$text = 'No birthdays yet';
} else {
$num = count($birthdays);

$i = 1;

foreach($birthdays as $b) {
$date = explode("-", $b["user_birthday"]);
$user = get_user($b["user_id"]);
$user_menus[] = create_user_popup($user);
if($date[2] == date('j')) {
$text.= "<img src='" . BASE_DIR . "/birthday.gif' title=\"User's birthday today!\"/>";
$dateprint = '<span style=\'color:red\'><strong>TODAY!</strong></span>';
} else {
$dateprint = date('F jS', mktime(0, 0, 0, $date[1], $date[2], $date[0]));
}
$text .= user_link_code($b["user_id"], $b["user_name"]);
$text .= " (" . $dateprint . ")";
if($num > $i) {
$text .= ', ';
}
$i++;
}

}



/**** BEGIN BIRTHDAYS TODAY ****/
function get_birthdays() {
$result = db_query("SELECT user_id, user_name, user_birthday FROM wowbb_users
WHERE DAYOFMONTH(user_birthday) >= DAYOFMONTH(NOW()) AND MONTH(user_birthday) = MONTH(CURRENT_DATE) AND DAYOFMONTH(user_birthday) < DAYOFMONTH(CURRENT_DATE)+7
ORDER BY DAYOFMONTH(user_birthday) ASC;");
while($row = db_fetch_row($result)) {
$birthdays[] = $row;
}
return $birthdays;
}

/**** END BIRTHDAYS TODAY ****/

Fumigator
06-12-2007, 04:40 AM
Well the query seems to ignore the year on the date already. Your problem is probably happening because the birth year being entered is earlier than 1970, which is the lowest value that the mktime() function can handle in PHP.

ClassicNancy
06-12-2007, 04:43 AM
So what is a fix for that?:confused:

Fumigator
06-12-2007, 04:50 AM
I've had this function kicking around for forever... use it just like you'd use mktime().



function MakeTime() {
$objArgs = func_get_args();
$nCount = count($objArgs);
if ($nCount < 7)
{
$objDate = getdate();
if ($nCount < 1)
$objArgs[] = $objDate["hours"];
if ($nCount < 2)
$objArgs[] = $objDate["minutes"];
if ($nCount < 3)
$objArgs[] = $objDate["seconds"];
if ($nCount < 4)
$objArgs[] = $objDate["mon"];
if ($nCount < 5)
$objArgs[] = $objDate["mday"];
if ($nCount < 6)
$objArgs[] = $objDate["year"];
if ($nCount < 7)
$objArgs[] = -1;
}
$nYear = $objArgs[5];
$nOffset = 0;
if ($nYear < 1970)
{
if ($nYear < 1902)
return 0;
else if ($nYear < 1952)
{
$nOffset = -2650838400;
$objArgs[5] += 84;
// Apparently dates before 1942 were never DST
if ($nYear < 1942)
$objArgs[6] = 0;
}
else
{
$nOffset = -883612800;
$objArgs[5] += 28;
}
}

return call_user_func_array("mktime", $objArgs) + $nOffset;
}

ClassicNancy
06-12-2007, 04:52 AM
not sure how to do that but will look into it. Thank you.

ClassicNancy
06-12-2007, 04:56 AM
Can you help out an old lady and tell me what I need to do with that code??

Fumigator
06-12-2007, 05:13 AM
Put it in a PHP file and then use require() (http://us2.php.net/manual/en/function.require.php) in your script to use it. Then just call the MakeTime() function in place of PHP's mktime() (http://us2.php.net/manual/en/function.mktime.php) function.

ClassicNancy
06-12-2007, 05:16 AM
oh ok. Thank you.:thumbsup:

ClassicNancy
06-12-2007, 07:07 AM
Ok I did that and yes now if the year is there and it is before 1970 it shows the right month and day. BUT, now if they do not have the year added it does the December 31st date. It just reversed the problem. Is there no way at all to tell it to ignore the year??

Fumigator
06-12-2007, 03:47 PM
I don't follow when you say "when they don't enter a year". I thought the tech guys from the forum you're using said the year is required? And isn't this birthday stored in a MySQL database as a DATE type value? So you've got to have a year for that value.

To answer your question directly-- mktime() (and that custom MakeTime() function) don't require a year value (they will default to current year if it is omitted), but a DATE type value stored in a MySQL database must have a year value.

ClassicNancy
06-12-2007, 03:58 PM
There is an option in the drop down in profile that just says year and if you choose that the system allows it. So when a member has that setting that is where the problem arises. I can't win either way, lol. :( i tried going in and adding the birthday in the array for required fields on registration but haven't got it to work yet and then if it does I'm not sure it will matter because the same year options like the name year will still be there. Unless I can get that out too. But then if anyone else wants to use this mod they have to go through the same hoops so really wanted to fix of some kind beside having to change 10 more things, lol

Fumigator
06-12-2007, 04:34 PM
Just validate the year-- if it's not between 1900 and 2100 then use current year.

ClassicNancy
06-12-2007, 04:39 PM
Ok:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum