Php 值($age)返回意外结果

Php 值($age)返回意外结果,php,mysql,return,var,floor,Php,Mysql,Return,Var,Floor,理想情况下,应该发生的是,基于游戏中的日期,网站设置为(当前为3014-11-16),网站应该计算角色的当前年龄并返回该年龄。因此,如果我们将年份设置为忽略年份为10,将字符的出生日期设置为2,则我们的年龄为8岁 当前日期设置为:3014-11-16(yyyy-mm-dd) 木狼的出生日期为:2998-01-12(yyyy-mm-dd) 他的年龄应该显示为16岁,而不是-55岁 如果我把他的出生日期设为:3013-11-16,他的年龄就会变为-71岁 如果我把他的出生日期设为:3012-11-1

理想情况下,应该发生的是,基于游戏中的日期,网站设置为(当前为3014-11-16),网站应该计算角色的当前年龄并返回该年龄。因此,如果我们将年份设置为忽略年份为10,将字符的出生日期设置为2,则我们的年龄为8岁

当前日期设置为:3014-11-16(yyyy-mm-dd) 木狼的出生日期为:2998-01-12(yyyy-mm-dd)

他的年龄应该显示为16岁,而不是-55岁

如果我把他的出生日期设为:3013-11-16,他的年龄就会变为-71岁 如果我把他的出生日期设为:3012-11-16,他的年龄会回到-70岁 如果我把他的出生日期设为:3005-11-16,他的年龄会从7岁变为-63岁 如果我把他的出生日期设置为:2998-11-16,他的年龄会从16岁变为-56岁

如果我把他的出生日期设为:2945-11-16,他的年龄会回到-3岁 如果我把他的出生日期设为:2944-11-16,他的年龄会回到-1 如果我把他的出生日期设置为:2943-11-16,他的年龄会回到0

如果我把他的出生日期设为:2105-11-16,他的年龄会从909岁变回837岁

我试图通过删除大量与问题无关的密钥数组和部分来最小化代码。如果我在最后一行将
$age
设置为
$age=15
,我会得到15,因此我知道我正在尝试修改返回到
$age
变量的值,但在多次尝试后我完全迷失了方向

我正在寻找一种方法来获得角色的正确年龄。这是我的目标,希望有人能看到我做错了什么

以下是相关代码:

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...
require_once('../include/Application.php');//我们需要包含的唯一类
$app=新应用程序();//创建初始应用程序对象
$app->connect();//连接到mysql服务器
$sess=新会话();//开始新的会话
$current=新的CurrentUser(错误);
如果($current->errors){
$errors=$current->errors;
触发错误(当前($errors),E\u用户错误);
}
包括(“../functions.php”);
$id=“”;
$record=array();
认可变量(数组(“id”);
$PHP_SELF=$_服务器[“PHP_SELF”];
$errors=array();
//从数据库获取日时间
$daytimes=新的DaytimeList();
如果($daytimes->errors){
$errors=$daytimes->errors;
触发错误(当前($errors),E\u用户错误);
}
//从数据库中获取当前游戏
需要_一次('../include/Game.php');
$cu_game=新游戏();
$cu_游戏->选择当前();
如果($cu_game->errors){//查询错误
$errors=$cu_游戏->错误;
触发错误(当前($errors),E\u用户错误);
}elseif($cuu-game->id){//我们得到了当前的游戏
列表($gametime,$realtime,$daydlength,$inittod)=数组(htmlspecialchars($cu_game->gametime,ENT_引号),htmlspecialchars($cu_game->realtime,ENT_引号),$cu_game->daydlength/604800,htmlspecialchars($daytimes->数据[$cu_game->inittod],ENT_引号));
}
/*
$GameTimFile=“$basefile/gamedate/gamedate.txt”;
$tempy=file($gametimefile);
$gametime=trim($tempy[0]);
$realtime=trim($tempy[1]);
*/
如果($id){
//比赛时间公式
//从天(到天(“$gametime”)+地板(到天(NOW())-到天(“$realtime”)/14))
如果(!是数值($id)){
$characterSQL=“SELECT*,DATE_FORMAT(lastmodified,%b%e@%h:%i%p”)作为modformat,TO_DAYS(NOW())-TO_DAYS(lastmodified)作为modage,IFNULL(FLOOR((TO_DAYS('gametime'))+FLOOR((TO_DAYS(NOW())-TO_DAYS('realtime'))/14)-TO_DAYS(dateofbirth))/365.25),0)作为年龄,dateofbirth格式(dateofbirth),%b%e,%Y'))作为dateofbirth,datecreated作为dateadded从大脑中添加,其中代码名=\“”.mysql\u real\u escape\u string($id)。“\”;
}否则{
$characterSQL=“SELECT*,DATE_FORMAT(lastmodified,%b%e@%h:%i%p”)作为modformat,TO_DAYS(NOW())-TO_DAYS(lastmodified)作为modage,IFNULL(FLOOR((TO_DAYS('gametime'))+FLOOR((TO_DAYS(NOW())-TO_DAYS('realtime'))/14)-TO_DAYS(dateofbirth))/365.25),0)作为年龄,dateofbirth格式(dateofbirth),%b%e,%Y'))作为dateofbirth,datecreated作为dateadded从大脑中添加,其中id=\“”.mysql\u real\u escape\u string($id)。“\”;
}//如果是,则结束
$result=mysql\u查询($characterSQL,$db);
如果(!mysql_num_rows($result)){errorpage(“没有具有该id号的记录。请检查用于访问此页面的链接并重试,或者检查以查找您正在查找的记录。”);die;}
$record=mysql\u fetch\u assoc($result);
$id=$record[“id”];
}否则{
标题(“位置:/cerebra.php”);
die;}//end if ID
//“型号名称”、“歌曲”、“歌曲链接”
//“模型”、“歌曲”、“歌曲链接”,
$fields=数组(
“年龄”=>“年龄”,
“公寓时代”=>“公寓时代”,
“操作基础”=>“操作基础”,
“出生日期”=>“出生日期”
);
$patterns=array(“/\”/”,“/[\n\r\f]+/”;
$replace=array(“,”

\n”); FOREACH($label=>$field形式的字段){ ${$field}=preg_replace($patterns,$replace,trim($record[$field]); ${$field.“Name”}=$label; }//结束FOREACH 如果($age>“2000”){$age=“”;} //如果我在这里设置$age=15,那么字符年龄将显示为15。。。
您可以在这里看到一个结果示例:这里的信息既不够,又太多。请用数据库中的一些实际数据发布年龄计算结果。我将尝试找出如何进一步缩减,并发布更多相关数据。这可能需要我一天的时间来完成,因为我不太确定该怎么做(我对数据库一无所知,正在处理其他人编写的代码)。在您的查询中,您的问题似乎是
$realtime
到\u天(“$realtime”)
。使用您的示例结果,如果我将其设置为
3014-11-16
->
to_DAYS('3014-11-16')
,那么我将得到您当前的结果,但是如果我将其更改为
to_DAYS(NOW())
,请参见,那么我将得到您想要的结果。因此,请查看
$cu\u game->realtime
/
$realtime
,看看它是否与
$cu\u game->gametime
/
$gametime