How do I update a data when the inserted date is equal to the current date?

1

I am making a system where the user can schedule postings, that is, if he enters the date and time, the post will be released when the current date is equal to inserted, until there logic is simple but I am in trouble to apply this to a function.

I watched a videoaula , but it is very old and uses parameters that I could not use so I adapted the code but anyway does not work.

The function looks like this:

function publicarAgendado(){
  date_default_timezone_set('America/Sao_Paulo');
  $dataAgora = date('d-m-Y H:i:s');

  $selecionar_agendados = @mysqli_query("SELECT * from postagens where status = 0");

  while ($PubAgd = @mysqli_fetch_array($selecionar_agendados)) {
    $aiai = $PubAgd['id'];
    setlocale(LC_TIME,'pt_BR','pt_BR.utf-8','portuguese');
    date_default_timezone_set('America/Sao_Paulo');
    $data_banco = date('d/m/Y H:i:s', strtotime($PubAgd['agendado']));
      if ($data_banco <= $dataAgora) {
        $up = mysqli_query("UPDATE postagens SET status=1 where id= $aiai");

      }

   } 
}

In the database the field of this date is as datetime and the HTML is in an input with the datetime-local type. Please help me urgently as I have to submit a preview of this system tomorrow.

    
asked by anonymous 26.09.2018 / 23:32

2 answers

0
  

Assuming the database connection link is:

     

$conn = new mysqli ("localhost", "USUARIO", "SENHA", "Nome_DB");

  • Just use date_default_timezone_set('America/Sao_Paulo'); only once at the start of PHP.
  • In querys (select and update) the connection link with the database mysqli_query($conn,"SELECT... and mysqli_query($conn,"UPDATE is missing.
  • Return the date of the bank in the original format and set dataAgora to the same format, ie $dataAgora = date('Y-m-d H:i:s');
  • No more the correct code is:

    function publicarAgendado(){
    
        $conn = new mysqli ("localhost", "USUARIO", "SENHA", "Nome_DB");
    
        date_default_timezone_set('America/Sao_Paulo');
        setlocale(LC_TIME,'pt_BR','pt_BR.utf-8','portuguese');
    
        // data compativel com a data do banco
        $dataAgora = date('Y-m-d H:i:s');
    
        $selecionar_agendados = @mysqli_query($conn,"SELECT * from postagens where status = 0");
    
        while ($PubAgd = @mysqli_fetch_array($selecionar_agendados)) {
           $aiai = $PubAgd['id'];
           //basta retornar a data do banco no seu formato original
           $data_banco = $PubAgd['agendado'];
    
           if ($data_banco <= $dataAgora) {
              $up = mysqli_query($conn,"UPDATE postagens SET status=1 where id= $aiai");
           }
    
    }
    
    //Execução normal automatica
    publicarAgendado();
    
  •   

    NOTE: Do not use $dataAgora = date('d-m-Y H:i:s'); to compare with $data_banco = date('d/m/Y H:i:s, strtotime($PubAgd['agendado'])); that will not work on dates of the same day with the time before the current time. The explanation? Do not know. I tested and found out!

    date_default_timezone_set Sets the default time zone used by all date functions and time in a script.

        
    27.09.2018 / 03:59
    0

    Good evening,

    Do this and verify that it has been resolved:

    $data_banco = date('d-m-Y H:i:s', strtotime($PubAgd['agendado']));
    
        
    27.09.2018 / 03:52