user1973756
user1973756

Reputation:

How to run a PHP function 1 minute from now?

I have MySQL service on Windows Server. I have a script in PHP, when the user runs it, it should stop the MySQL service, and then after 60 seconds it must start the service again. I wrote some code like this:

$stopTime = time();
$now = date("H:i", time());
$mustRun = date("H:i", intval($stopTime)+60);

while(trim(strval($now)) !== trim(strval($mustRun))) {
    continue;
} // now it should be start mysql service

I saved the running time in the $stopTime variable, and calculated the 60 seconds after using $mustRun variable. I want to start the service when the current time is $mustRun, How can I do that?

Upvotes: 1

Views: 5566

Answers (5)

gafreax
gafreax

Reputation: 410

You can also try this function that use a callback called after specified time

function delay_exec($function_name,$param,$delay) {
    $ret = null;
    if (!is_int($delay) || !is_function($function_name)) {
        echo "<h2>Errore </h2>"
        return $ret;
    }
    sleep($delay);
    $ret = call_user_func_array($function_name,$param);
    return $ret;
}

So if u have foo($a,$b) to call, you can call like this way:

delay_exec('foo',array($a,$b),60);

Upvotes: 1

Ashouri
Ashouri

Reputation: 906

you can also use cronjob in your server

This method is more reliable because in some cases it may be due to the security settings of webserver ,your code do not correct work

look at this link

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

Upvotes: 1

randomizer
randomizer

Reputation: 1649

Use 'sleep' in combination with outputbuffering. Something like:

<?php
ob_start();
$buffer = str_repeat(" ", 4096);

for ($i=0; $i<10; $i++) {
  echo $buffer.$i;
  ob_flush();
  flush();
  sleep(60);
}
ob_end_flush();
?>

Without using outputbuttering, 'sleep' won't show the behaviour as expected.

Upvotes: 1

doublesharp
doublesharp

Reputation: 27667

Use the 'sleep()' or 'usleep()' method instead of a while loop, pass in the amount of time you want to pause.

Upvotes: 0

bozdoz
bozdoz

Reputation: 12890

Perhaps you can try php's sleep.

<?php

// sleep for 60 seconds
sleep(60);

?>

Upvotes: 1

Related Questions