value ($age) returns unexpected result

55 views Asked by At

Ideally what should happen is, based on the in-game date that the site is set to (currently its 3014-11-16) the site should calculate a characters current age and return that age. So if we set the Year to ignore year to 10 and a characters Date of Birth to 2, we would get an age of 8.

The current date is set to: 3014-11-16 (yyyy-mm-dd) The date of birth for timber wolf is: 2998-01-12 (yyyy-mm-dd)

His age should display as 16, instead it displays as -55

If i set his DOB to: 3013-11-16, his age become -71 If i set his DOB to: 3012-11-16, his age come back -70 If i set his DOB to: 3005-11-16, his age comes back as -63 instead of 7 If i set his DOB to: 2998-11-16, his age comes back as -56 instead of 16

If i set his DOB to: 2945-11-16, his age come back as -3 If i set his DOB to: 2944-11-16, his age comes back as -1 If i set his DOB to: 2943-11-16, his age comes back as 0

If i set his DOB to: 2105-11-16, his age come back 837 instead of 909

I've tried to minimize the code by removing a lot of the key array and parts that are not related to the problem. If I set the $age at the very last line to $age=15, I get fifteen, so I know that I am trying to revise the value being returned to the $age variable, but I'm completely lost after many attempts.

I'm looking for a way to get the correct age of the character. Thats my goal, hopefully someone out there can see what i'm doing wrong.

Here is the relevant code:

require_once('../include/Application.php'); // The only class we need to include
$app = new Application(); // Create the initial application object
$app->connect(); // Connect to the mysql server
$sess = new Session(); // Start a new session
$current = new CurrentUser(FALSE);
if ($current->errors) {
    $errors = $current->errors;
    trigger_error(current($errors), E_USER_ERROR);
}

include ("../functions.php");

$id="";
$record=array();

approveVars(array("id"));
$PHP_SELF = $_SERVER["PHP_SELF"];

$errors = array();

// Get daytimes from db
$daytimes = new DaytimeList();
if ($daytimes->errors) {
    $errors = $daytimes->errors;
    trigger_error(current($errors), E_USER_ERROR);
}

// Get the current game from the db
require_once('../include/Game.php');
$cu_game = new Game();
$cu_game->selectCurrent();
if ($cu_game->errors) { // Query error
    $errors = $cu_game->errors;
    trigger_error(current($errors), E_USER_ERROR);
} elseif ($cu_game->id) { // We got the current game
    list($gametime, $realtime, $daylength, $inittod) = array(htmlspecialchars($cu_game->gametime, ENT_QUOTES), htmlspecialchars($cu_game->realtime, ENT_QUOTES), $cu_game->daylength/604800, htmlspecialchars($daytimes->data[$cu_game->inittod], ENT_QUOTES));
}

/*
$gametimefile = "$basefile/gamedate/gamedate.txt";
$tempy = file($gametimefile); 
$gametime = trim($tempy[0]);
$realtime = trim($tempy[1]);
*/

if ($id) {

// FORMULA FOR GAME TIME
// FROM_DAYS(TO_DAYS('$gametime') + FLOOR((TO_DAYS(NOW()) - TO_DAYS('$realtime')) /14))

if (!is_numeric($id)) { 

$characterSQL = "SELECT *, DATE_FORMAT(lastmodified,'%b %e @ %h:%i %p') AS modformat, TO_DAYS(NOW()) - TO_DAYS(lastmodified) as modage, IFNULL(FLOOR((TO_DAYS('$gametime') + FLOOR((TO_DAYS(NOW()) - TO_DAYS('$realtime')) / 14) - TO_DAYS(dateofbirth)) / 365.25) ,0) AS age, DATE_FORMAT(dateofbirth, '%b %e, %Y') AS dateofbirth, datecreated AS dateadded FROM cerebra where codename=\"". mysql_real_escape_string($id) . "\"";

} else {

$characterSQL = "SELECT *, DATE_FORMAT(lastmodified,'%b %e @ %h:%i %p') AS modformat, TO_DAYS(NOW()) - TO_DAYS(lastmodified) as modage, IFNULL(FLOOR((TO_DAYS('$gametime') + FLOOR((TO_DAYS(NOW()) - TO_DAYS('$realtime')) / 14) - TO_DAYS(dateofbirth)) / 365.25) ,0) AS age, DATE_FORMAT(dateofbirth, '%b %e, %Y') AS dateofbirth, datecreated AS dateadded FROM cerebra where id=\"" . mysql_real_escape_string($id) . "\"";

} // end if'

$result=mysql_query($characterSQL, $db);
if (!mysql_num_rows($result)) { errorpage("There is no Cerebra record with that id number. Please check the link you used to access this page and try again, or check the <a href='/cerebra.php'>Cerebra main page</a> for the record you're looking for."); die; }
$record = mysql_fetch_assoc($result);

$id = $record["id"];

} else {
header("location: /cerebra.php");
die; } // end if ID


// "modelname","song","song_link"
//"Model", "Song", "Song Link",

$fields = array(
"Age"=>"age",
"Apparentage&nbsp;Age"=>"apparentage",
"Base Of Operations"=>"baseofoperations",
"Birthdate"=>"dateofbirth"
);


$patterns = array ("/\"/","/[\n\r\f]+/");
$replace = array ("&quot;","</p>\n<p>");

FOREACH ($fields as $label => $field) {
${$field} = preg_replace($patterns, $replace, trim($record[$field]));
${$field . "Name"} = $label;
} // end FOREACH

if ($age > "2000") { $age =""; }

//if i set $age = 15 here, then character age will appear as 15...
0

There are 0 answers