Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP's version of JavaScript's SetTimeOut

Status
Not open for further replies.

Kharas

Programmer
Jan 30, 2007
39
0
0
MX
Greetings:

I am building an advanced countdown timer and am currently stuck in the following bit:

Basically what it 'did' in javascript was to get now's date with date(); every second with the use of the SetTimeOut function. However, for reliability, I was forced to adjust the timer to the server's clock using PHP's date("format");

The problem is now that every time JS tries to recalculate the time, my PHP date values are static and the clock doesn't tick anymore. So I've been searching for PHP's alternate version of JavaScript's SetTimeOut which would ultimately allow me the recalculation of the 'forced' values =)

Code:
function GetCount()
dateFuture = new Date(2007,7,10,2,0,0);
dateNow = new Date(2007,7,10,1,0,0); //Here is where I inserted the <?php ?> tags with the static values
amount = dateFuture.getTime() - dateNow.getTime(); 
	delete dateNow;
	days=0;hours=0;mins=0;secs=0;out="";

		
		amount = Math.floor(amount/1000);

		

		hours=Math.floor(amount/3600);
		amount=amount%3600;

		mins=Math.floor(amount/60);
		amount=amount%60;

		secs=Math.floor(amount);

		out += "Next Match In: ";
		
		if(days != 0 || hours != 0){out += ((hours<10)?"0":"") + hours + ":";}
		if(days != 0 || hours != 0 || mins != 0){out += ((mins<10)?"0":"") + mins + ":";}
		out += ((secs<10)?"0":"") + secs + " EST";
		
		document.getElementById('countbox').innerHTML=out;

		setTimeout("GetCount()", 1000);
Thanks in advance
 
Hi

I do not understand that. When should the JavaScript synchronize with the PHP ? Anyway, I see no reason to do any delayed execution in PHP and neither is possible like you think. Your closest option would be to [tt]pcntl_fork()[/tt] and [tt]sleep()[/tt], but probably is completely useless, unless the server is your.

If you thought to AJAX based synchronization ( which of course still not implies any delayed task ), then try forum1600.

Feherke.
 
Kharas, I think that your concept is flawed.

The starting time taken with PHP will never be the real starting time because there is an unknown delay between the PHP time and the time when the Javascript starts to run.

Anway, the PHP version of SetTimeOut is sleep() :
 
I only showed parts of the code so I wouldn't flood the thread so fast.

The reason I'm using PHP's date, regarldess of the delay, which in THIS particular case does not represent any loss, or problem, or flaw, is because many many users do not have their computer clock's properly set.

Which would result in a misleading clock should I calculate the times I want basing myself on their local time.

If the clock gets a few seconds of delay with PHP, it is more than perfect.

Now, as for the coding part:

<?php

$year = 0;
$month = 0;
$day = 0;
$hour = 0;
$min = 0;
$sec = 0;

$year = date("Y");
$month = date("m")-1; /* The JS function requests you use months - 1 */
$day = date("j");
$hour = date("G")+4; /* Converting my server's time to EST*/
/* chain of if statements to make sure 24,25,26 and 27 get reconverted to 0,1,2 and 3 after the addition of the four hours, no need to see that */
$min = date("i");
$sec = date("s");

?>

dateFuture = new Date(2007,7,10,2,0,0); // Date we're counting down to
dateNow = new Date(<?php echo $year.",".$month.",".$day.",".$hours.",".$min.",".$sec); // Current Date, obtained through php date()

Now the problem here is that the values I get from PHP are STATIC, and when the JS CountDown function reloads with SetTimeOut, the clock WILL NEVER tick because the dateNow var remains the same, hence my question about the reloading function about php.

Here I 'placed' the isolated clock function without any php for you to understand all my blabla:


P.S. The server is indeed mine

Thanks yet again
 
Hi

Well, the JavaScript can calculate the time between request and response, so can calculate an approximation.

I did not read it yet, but I am sure that RFC 4330 contains some better hints.

Sleidia said:
Jeez ... you're too fast Feherke ;)
Yeah, sure. I replied after 1 hour and 11 minutes. :-( Maybe learning more English would help...

Feherke.
 
You see, the whole purpose of the clock is to give the 'users' of the website an idea of when a certain event is taking place. They already know the exact time, it is just a fresh reminder.

I'd rather have an hour-wise accurate clock using php date() than have second-wise accurate clock, yet very possibly hour-wise inaccurate JS clock that relies upon the user's machine time.

So, putting the accuracy aside, sleep() should do it?
 
i can't see how sleep would help. server interaction does not work like that.

i'd rather see this kind of construction:

1. use sessions server side
2. set a timestamp into a session variable on the server
3. set a timer (say 6000 milliseconds) in js
4. every js timeout send an ajax ping to the server (it does not matter if this is inaccurate from time to time)
5. the server should return the elapsed time in some suitable format
6. the client displays the newly received timestamp.

if session variables don't work for you, you can always hook the value from a database on each ajax call, the latency will be negligible.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top