Timer in PHP

2

I need to implement a feature, in which a certain code runs several times after a time interval.

For example, read files made available by the API every 20 minutes. I tried something of a genre, but I have not tried it, I just want to have an easy and sure way to do it.

   while(true)
   {
      // Executa o codigo aqui;
      usleep(1200);

   }

Does PHP have some method for scheduling tasks? What would be the mechanism to cancel loop , if the above idea is valid?

    
asked by anonymous 23.03.2016 / 10:59

1 answer

1

Cron job is the most correct way to do it;)

Call an index.php every minute:

1 * * * * wget www.meusite.pt/crons/index.php

Then inside that php file is just an "ifs":

ore_user_abort();
set_time_limit(3600);

// a cada hora
if(date('i')==59){
    $this->hour();
}

// a cada minuto
$this->minute();

Something out there ... Any questions please;)

Hugs and good projects!

    
23.03.2016 / 12:12