How can I make a timer in php?

I need to make a timer for php but sleep, unsleep, microtime or set_time_limit don’t work. I understand that this is a burden on the system but I need it. Or is there some other system for timer that would run php code?

Hello

Please make sure to use the correct category next time.

PHP functions that pause a script from running are not supported here, and they require too much server power, amoung other things.

Instead, use MySQL Databases to keep track of time, or JavaScipts to show an actual timer on-screen.

——

If you give us more information about our end goal, we will certainly be able to help you further.

7 Likes

There are a few different ways to make a timer in PHP. Here is one way you can do it:

Copy code

$start_time = microtime(true);

// code to be timed goes here

$end_time = microtime(true);
$elapsed_time = $end_time - $start_time;
echo "Elapsed time: $elapsed_time seconds";

This will output the amount of time, in seconds, that it took for the code between $start_time and $end_time to execute.

Alternatively, you can use the date function to create a timer that counts down to a specific date and time. Here is an example of how you could do this:

Copy code

$target_time = "2022-01-01 00:00:00";

while (time() < strtotime($target_time)) {
  $current_time = time();
  $time_left = strtotime($target_time) - $current_time;
  echo "Time left: $time_left seconds\n";
  sleep(1); // sleep for 1 second
}

This code will output the time left, in seconds, until the target time is reached. It will also pause for 1 second between each iteration of the loop.

Both examples missed the post, they use sleep and microtime which were mentioned to not work:

7 Likes

Please make sure to add code that is relevant to the question asked by OP. This is certainly not relevant as it contains many functions that have been disabled on InfinityFree as stated by @ChrisPAR.

5 Likes

If you are unable to use the sleep(), usleep(), microtime(), or set_time_limit() functions, you can try creating a timer using a loop that checks the current time at regular intervals and exits when the desired time has elapsed.

Here is an example of how you could do this:

Copy code

$start_time = time(); // get the current time
$duration = 60; // set the duration of the timer in seconds

while (time() < $start_time + $duration) {
    // code to be executed goes here
}

This will create a loop that runs for 60 seconds (assuming that $duration is set to 60). You can adjust the duration by changing the value of $duration.

Note that this method may not be as accurate as the other functions, as it relies on the server’s clock and may be affected by any delays in the script’s execution. However, it may be a suitable alternative if the other functions are not available or not suitable for your needs.

What do you mean exactly by creating a “timer” in PHP.

Continuously running background tasks are not allowed on our hosting, and many other web hosting providers for that matter. And if you want to schedule background tasks, you can use Cron Jobs for that.

The sleep() and set_time_limit() functions are disabled because they both can result in keeping connections open and scripts running for a very long time.

microtime() on the other hand doesn’t appear to be disabled, and I don’t see why it would be. What trouble are you having using it?

5 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.