Run function in PHP to insert data into DB daily at specific time

1

I am creating a "module" of my system and now I have to create a function that, when it arrives at the time that is in the database, execute a insert . For example, if the table is the 22:00 time, at 10:00 pm every day it will run insert .

The problem is that sometimes a delay or something like this can happen and the current time is 22h01 , and since it is in the 22h00 database, it will not execute the insert.

I was thinking about putting a break. If, for example, the bank is 22:00 it puts the interval of 1 hour, then if it is 10:00 PM until 11:00 PM it executes the insert. I have no idea how to do it, can anyone help me with this PHP code?

NOTE: I will also put a CRON to help.

    
asked by anonymous 29.12.2015 / 01:14

3 answers

5

This is a very simple but fully functional path for this type of problem:

  • create a proximaexecucao field in your DB's schedule table

In PHP you will use the following algorithm:

  • If the current date / time greater than or equal to proximaexecucao :

    • Do the scheduled operations you want

    • resets proximaexecução to:

      • the next day if you want the delayed to run once

        OU

      • One day more than proximaexecução current, if you want ALL arrears to run.

  • If the current date / time is less than proximaexecucao

    • returns without doing anything

In this way, you will have the execution running without the risk of double-execution, and without depending on comparisons of minutes, or the availability of a CRON with a very short interval of time.

This solution can be applied to many different schedules, with different intervals, as long as each has a proximaexecucao field.

And "toast" has the advantage of being able to change the schedules remotely without touching the crontab of the machine, just adjust in the DB. If you put a call via cron every 5 minutes, you can schedule a variety of different events to happen during the day, at various times, all of which are executed by the same PHP. >     

29.12.2015 / 01:44
0

Logic would be something like this

<?php

$agendamento = 22; // Não vou usar minutos aqui
$horaAtual   = ; // Obtenha a hora via JS

if ( ( $agendamento >= $horaAtual ) || ( $agendamento <= $horaAtual) ) {

    # Execute alguma coisa
}
else
{
    # Ainda não é hora
}

 ?>
    
29.12.2015 / 01:54
0

Cron tasks, or Cron job, have in the host normally, only ask information in the support, when to learn, install in your pc / note, windows are the common tasks, and linux is cron if I am not mistaken the name .

Then you have to run a script.php from time to time to check something and execute what you want.

I used it for my shift game, every 20 minutes I changed the shift, then ran the engine.php

    
29.12.2015 / 15:36