How can I run a PHP script every second?

I am currently writing an online game. Now I have to check if an event has occurred (checking the timestamp in the database) and depending on what actions are being performed. I have to check the event every second.

I wanted to use cronjob, but with cron you can run a script only every minute.

My idea was to use cron and loop 60 times in my php script. But I think this is not the best solution.

So what is the best way to run a script every second?

0
source share
5 answers

I was looking for a better solution, but it seems like my first idea with clean code is a better solution.

This is my current code:

<?php
set_time_limit(60);
$start = time();

for ($i = 0; $i < 59; ++$i) {
    // Do whatever you want here
    time_sleep_until($start + $i + 1);
}
?>
0
source

script , ? .

0

, script . , , ( ) .

0

: P. cronjob, ? , , . - .

0
$total_time = 0;
$start_time = microtime(true);
while($total_time < 60)
  {
        //DoSomethingHere;
    echo $total_time."\n";
    //sleep(5);
  $total_time =  microtime(true) - $start_time ;
  } 

crontab .

0

Source: https://habr.com/ru/post/1534444/


All Articles