Hi there,

I'm currently trying to write a function that works on a 24 hour (obviously) day but it must run 4x the normal speed.

So as an example, hour: 22 will pass 4 times every day.

It doesn't need to work on a AM/PM basis but only hour 1-24.

This may sound strange but it is for a php-based RPG game which is why the day needs to run 4x quicker so that it doesn't bias a particular timezone in the world.

My dev function is this but it doesn't work correctly.

function zone_time_check()
{
	global $db, $phpEx, $board_config;

	$diff = (time() *4);
	$hour = 1;
	$sec_in_an_hour = 60 *60;
	while($diff >= $sec_in_an_hour)
	{
		$hour++;
		$diff -= $sec_in_an_hour;
	}

return $hour;
}

This is probably going to be an easy fix but I think I've been looking at it for too long now to see where I'm erroring.

Thanks in advance!. :)

What an interesting puzzle, this is. I stumbled across it and couldn't help but get drawn into solving this.

The best way I can think to tackle this is to work in seconds and then work upwards from there.
So you would start with a particular starting time, where the time corresponds exactly. For this example, let's use 2000-01-01 00:00

Okay, so you need to find the unix epoch (number of seconds since 1970) for 2000-01-01 00:00
The unix epoch is 946684800
Next you will then need to decide the number of seconds that have elapsed since then and multiply them by 4

So 60 seconds later, the unix epoch will be 946684860
In virtual time, this would be 240 seconds later.

$start_epoch = 946684800; /*theoretically this could be in the future if you wanted to start off in the past and work to a synchronised future date, such as 2015-01-01*/

$real_epoch = date("U"); /*current unix epoch*/

$time_ratio = 4; /* ratio - 4 times as fast in this case */

$virtual_epoch = (($real_epoch - $start_epoch) * $time_ratio) + $start_epoch; /* the time within the game */

$virtual_time = date("Y-m-d H:i:s", mktime(0 , 0, $virtual_epoch, 1, 1, 1970)); /* convert the epoch to a real date, using PHP's date() function */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.