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
https://forums.phpfreaks.com/topic/212579-php-timer/
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
https://forums.phpfreaks.com/topic/212579-php-timer/#findComment-1107454
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
https://forums.phpfreaks.com/topic/212579-php-timer/#findComment-1107458
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
https://forums.phpfreaks.com/topic/212579-php-timer/#findComment-1107623
Share on other sites

Are you trying to create a PHP function that works like JavaScripts setInterval or setTimeout methods?  If so you'll have to look into shared memory and pcntl_fork().

 

If you're trying to do this on Windows I recommend a different programming language.

Link to comment
https://forums.phpfreaks.com/topic/212579-php-timer/#findComment-1110775
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.