function prsInGameTime($strTimeLine) {
/**************
int prsInGameTime(string $strTimeLine)
Takes a full string like this:
Wed 4th Aug, year 1. Afternoon
$strTimeLine must be a string just like above, from the game.
This function will return the real world timestamp that corresponds to the in-game time line provided.
So what's the logic behind all this magic?
First calculate the unix timestamp for the first tick in "game time". [March 1st, 2007. 18:00:00 GMT]
Next, simplify the user's inputed time line [Thu 12th Aug, year 1. Evening] into something timestampable. [Aug 12th, 2007. 18:00:00 GMT]
Now subtract the two and you have the amount of seconds between the user's game date, and the first game date (Mar 1st).
Convert those seconds in to days. Now you know how many days have passed in the game from [March 1st, year 1] to the user's game date [Aug 12th, year 1].
Now you can safely convert the amount of game days that have passed, to the amount of hours that have passed.
1day(in-game) = 1hr(real-time)
Now you know how much (real) time has passed between first tick and user's tick.
Now calculate the unix timestamp for the first tick in "real time". [December 14th, 2007. 18:00:00 GMT]
Now if you subtract the current GMT time [time()] from the first tick (real time), you'll find out how many seconds have passed from the first tick to the current time (or last tick).
Now if you take the seconds from first tick to the current time, and subtract the amount of seconds from first tick to the user's time, that will tell you how many seconds ago the user's tick passed.
Then subtract the amount of seconds ago the user's tick passed from the current GMT timestamp.
And that, my friends, is the integer that is returned by this function.
The function will convert an internal bushtarion date in to it's real time unix timestamp.
***************/
$strTimeLine = trim($strTimeLine);
$strFirstTickg = "March 1st, 2007";
$strFirstTickr = "December 14th, 2007 18:00:00 GMT";
$intCurrTime = time(); //Current time in the UK
//First tick, in GAME time, represented as unix timestamp.
$intFirstTickg = strtotime($strFirstTickg);
//First tick, in REAL time, represented as unix timestamp.
$intFirstTickr = strtotime($strFirstTickr);
$x = explode(" ",$strTimeLine);
$arrTimeLine = array(
"day" => $x[1],
"month" => substr($x[2],0,-1),
"year" => (int)substr($x[4],0,-1),
"time" => $x[5]
);
if($x[6]) { $arrTimeLine['time'] = $arrTimeLine['time']." ".$x[6]; } // For times of day like: "Early Morning" and "Late Evening".
switch($arrTimeLine['year']) {
case 1:
$arrTimeLine['year'] = 2007;
break;
case 2:
$arrTimeLine['year'] = 2008;
break;
case 3:
$arrTimeLine['year'] = 2009;
break;
case 4:
$arrTimeLine['year'] = 2010;
break;
};
//Turn "Sat 7th Aug, year 1. Morning" in to "Aug 7th 2007"
$strNewTime = $arrTimeLine['month']." ".$arrTimeLine['day']." ".$arrTimeLine['year'];
//Convert user provided game time, in to a timestamp to compare with first tick's timestamp ($intFirstTickg).
$intNewTime = strtotime($strNewTime);
// Parse time of day here, adding 10min increments for the appropriate times of day.
/*********************************************
KEY NOTE: 10min of real time is 4 hours of game time!!
**********************************************/
if(strstr($arrTimeLine['time'],"Morning")) {
//This is the tick the game started on. Meaning these ticks represent the top of the hour!
}
if(strstr($arrTimeLine['time'],"Noon")) {
$intNewTime = strtotime('+4 hours',$intNewTime);
}
if(strstr($arrTimeLine['time'],"Afternoon")) {
$intNewTime = strtotime('+8 hours',$intNewTime);
}
if(strstr($arrTimeLine['time'],"Evening")) {
$intNewTime = strtotime('+12 hours',$intNewTime);
}
if(strstr($arrTimeLine['time'],"Midnight")) {
$intNewTime = strtotime('+16 hours',$intNewTime);
}
if(strstr($arrTimeLine['time'],"Dawn")) {
$intNewTime = strtotime('+20 hours',$intNewTime);
}
//How many (in-game)seconds between the first tick (game time) and the user provided tick?
$intUsrSecsSinceFirstTick = $intNewTime-$intFirstTickg;
//Take the seconds between first tick and user time, and convert those to represent days in the game. Which will double as real-world hours between first tick and the user provided time.
$intUsrHrsSinceFirstTickr = ((($intUsrSecsSinceFirstTick/60)/60)/24);
//Convert the real world hours between first tick and the user provided time ($intUsrHrsSinceFirstTickr), in to real world seconds.
$intUsrSecsSinceFirstTickr = $intUsrHrsSinceFirstTickr * 60 * 60;
//How many seconds between the first tick(real time) and right now?
$intCurrSecsSinceFirstTickr = $intCurrTime-$intFirstTickr;
$intSecsDiff = $intCurrSecsSinceFirstTickr - $intUsrSecsSinceFirstTickr; //How many seconds ago was the user time?
$intDaTStamp = $intCurrTime - $intSecsDiff;
if(DEBUG) {
echo "
This game time here: ".$strTimeLine;
echo "
...happened in real time @: ".date('l dS \of F Y G:i:sA e',$intDaTStamp);
exit;
}
return ($intDaTStamp);
}