Jump to content

PHP Timer


Twist3d

Recommended Posts

Hello.

Well i'm trying to make a timer to execute some code.

Basically I want 3 minutes, every 3 minutes a piece of code is activated, then another 3 minutes later it does so again.

But I have tried different ways of doing this, I tried with cookies and found out the person with the cookie is the only one that can refresh the page to activate the code.

Same with sessions.

And all the others are usually interrupted and start the timer again when the page reset.

 

So yeah, I don't want it to be interrupted by refreshing.

So any help please?

I also tried with timestamps, this has proven to be the best way but I can never get it working 100%

Link to comment
Share on other sites

You want that timer to run on one page and execute code every x seconds?

 

This is how you can start a timer:

$mtime = microtime(); $mtime = explode(' ', $mtime); 
$mtime = $mtime[1] + $mtime[0];  $starttime = $mtime;

 

Then every x seconds check:

$mtime = microtime();  $mtime = explode(" ", $mtime); 
$mtime = $mtime[1] + $mtime[0];  $endtime = $mtime; 
$totaltime = round($endtime - $starttime);

 

If total time >= 180, execute next piece of code. This would require you to keep the page open and output the results in it while it's loading (flushing).

 

You'd need following parameters for that:

ob_implicit_flush();
set_time_limit(0);

 

That would make the script work like you want to. But it's terrible coding practice. A better way would be to have a static page that fully loads but with AJAX functionality. Store your stuff in the database and let ajax execute a php script that compares the timestamps and execute code when necessary then output back to the browser. There's plenty of tutorials about ajax, and I've written a simple ajax code for someone else yesterday as well so you might find some inspiration there:

http://www.phpfreaks.com/forums/index.php/topic,309154.0.html

 

 

Link to comment
Share on other sites

You want that timer to run on one page and execute code every x seconds?

 

This is how you can start a timer:

$mtime = microtime(); $mtime = explode(' ', $mtime); 
$mtime = $mtime[1] + $mtime[0];  $starttime = $mtime;

 

Then every x seconds check:

$mtime = microtime();  $mtime = explode(" ", $mtime); 
$mtime = $mtime[1] + $mtime[0];  $endtime = $mtime; 
$totaltime = round($endtime - $starttime);

 

If total time >= 180, execute next piece of code. This would require you to keep the page open and output the results in it while it's loading (flushing).

 

You'd need following parameters for that:

ob_implicit_flush();
set_time_limit(0);

 

That would make the script work like you want to. But it's terrible coding practice. A better way would be to have a static page that fully loads but with AJAX functionality. Store your stuff in the database and let ajax execute a php script that compares the timestamps and execute code when necessary then output back to the browser. There's plenty of tutorials about ajax, and I've written a simple ajax code for someone else yesterday as well so you might find some inspiration there:

http://www.phpfreaks.com/forums/index.php/topic,309154.0.html

 

Hmm, Ill try that code first just before I get into Ajax, I'm only beginning working with timers and time and stuff in PHP so :P

So something like this should suffice? Or am I an idiot lol.

 

<?php
$mtime = microtime();  $mtime = explode(" ", $mtime); 
$mtime = $mtime[1] + $mtime[0];  $endtime = $mtime; 
$totaltime = round($endtime - $starttime);

ob_implicit_flush();
set_time_limit(0);

if ( $totaltime >= "180" ) {
echo "Done";
}
?>

Link to comment
Share on other sites

Hi all,

 

Sounds like your wanting your code minus the timer in a file, pop it on your server & then set up a cron job - that would be the best solution, unless this isn't on a server and your on localhost in which case, ignore me :)

 

Cheers,

Rw

Link to comment
Share on other sites

Hi all,

 

Sounds like your wanting your code minus the timer in a file, pop it on your server & then set up a cron job - that would be the best solution, unless this isn't on a server and your on localhost in which case, ignore me :)

 

Cheers,

Rw

 

This isn't on a server lol.

But for the first bit, no, I want the time to execute code both in the same file kind of like the above code.

Link to comment
Share on other sites

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.