Jump to content

Calculating a fictional date, not working in PHP 4.4.4


Recommended Posts

Hello!

 

I'm attempting to calculate the date for an online game that's set in the future. The games time is 4 times faster then our time so to figure it out I recorded the exact date/time in the game and the exact date/time in real time. Then all I need to do is multiply the seconds by 4 to the fictional date.

 

Here is what I currently have:

 

$realTime_start = mktime(17, 44, 20, 9, 25, 2007);
$realTime_current = mktime(date("H"), date("i"), date("s"), date("n"), date("j"), date("Y"));
$fomTime_start = mktime(14, 57, 0, 12, 8, 2007);

$seconds = ($realTime_current - $realTime_start) * 4;

$fom_date_full = date("H:i jS F Y",(strtotime("14:57 8 December 2007") + ($seconds)));
$fom_year = substr($fom_date_full, (strlen($fom_date_full) - 4), strlen($fom_date_full));
$fom_year = 2404 + ($fom_year - 2007);
$fom_date = date("H:i jS F",(strtotime("14:57 8 December") + ($seconds)));

$fom_date_full = $fom_date." ".$fom_year;

 

This works perfect in PHP 5+ but not on a server I'm using at the moment for testing (which uses PHP 4.4.4). I've referred to the manual to check the functions haven't changed from these versions and by the look there hasn't been any major changes.

 

At the time of writing this the correct output is "22:18 17th January 2405" and the PHP 4 output is "02:17 10th February 2367".

 

I would be grateful for any help, it's starting to hurt my brain :P

 

Thanks

Remember that a UNIX timestamp only has precision until 2038, http://en.wikipedia.org/wiki/Year_2038_problem

 

The number of the year, may be a two or four digit value, with values between 0-69 mapping to 2000-2069 and 70-100 to 1970-2000. On systems where time_t is a 32bit signed integer, as most common today, the valid range for year is somewhere between 1901 and 2038. However, before PHP 5.1.0 this range was limited from 1970 to 2038 on some systems (e.g. Windows).

 

But you're not using a timestamp to store the year, so most likely your clock is wrong.

You can't, a UNIX timestamp only has precision until 2038, http://en.wikipedia.org/wiki/Year_2038_problem

 

To calculate the year I've made an integer without using the mktime function. I don't think that's the problem as I have "2405" being printed locally and remotely on a PHP 5 server.

 

i'm guessing here but i think its the time of your server

 

I've checked the date and time config on the server and it's correct (except 4 hours behind). I've altered the script to suit that as the PHP 5 server was also 4 hours behind.

 

$fom_year = 2404 + (2007 - 2007); doesn't equal 2367. Confused...

Why make it so hard on yourself?

 

$realTime_current = mktime(date("H"), date("i"), date("s"), date("n"), date("j"), date("Y"));

 

Is the same as

 

$realTime_current = time();

 

 

 

Don't get me wrong (my PHP is appalling on some functions) but I think I need a specific format for this to work?

I'd suggest starting again for the date/time, you seem to be making it hard on yourself.

 

A unix timestamp the the no. of seconds after 1970.

$ourTime = time();
$gameTime = array(  'year' => date('Y', $ourTime) + 2404,
                    'month' => date('m', $ourTime),
                    'day' => date('d', $ourTime));

echo 'Game year is ' . $gameTime['year'];

Set it out something like that, you can change $ourTime to the start time, and just factor in how far in days, seconds, years or whatever the game time is ahead. No need to keep going back and forth.

I know it can be made shorter but that doesn't help. The game time is 4 times faster then ours so 1 day is 4 days in the game. To calculate that you need to calculate the amount of time lapsed from X date to current date * 4.

 

I'll have a play around with it though. Cheers.

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.