php - How to set maximum execution time, using interval in each loop? [duplicate]

0

Good morning! I need to send some email reminders according to some conditions. I put together a script that queries the information in the database (MySQL) and triggers the emails. The script is working very well, however, from what I saw in some forums on the internet, it is interesting to note a gap between sending one email and another. I put interval of 20 seconds between after each send, but before that I set a maximum execution time of the script, and from what I saw when I use the set_time_limit() function the sleep() function is ignored.

Follow the logic of my script below:

// faço a consulta na base de dados (MySQL)
if ($stmt->rowCount() > 0) {
   $result = $stmt->fetchAll();
} else {
   exit;
}

// controlar tempo máximo de execução do script
$qtd_email =  $stmt->rowCount();
$time_email = 20;
$segundos = ($qtd_email * $time_email) + 20; // adiciono 20 seg de folga
set_time_limit($segundos);

foreach ($result as $rs):

   // mensagem do e-mail

   // envia o e-mail

   // Ao terminar 1° loop, aguarda 20 segundos para o próximo
   sleep($time_email);

endforeach;

In this case, the sleep() function is being ignored. The script execution time on the server I'm going to host the application is at most 60 seconds, so I need to use set_time_limit() . If anyone knows a way to get around this, I'm very grateful!

If there is another way to do it, I'm also open to suggestions. Thank you in advance!

    
asked by anonymous 12.09.2018 / 17:09

0 answers