How to update a data when the date entered is equal to the current date?

Asked

Viewed 53 times

1

I’m making a system where the user can schedule posts, IE, if he enters the date and time, the post will be released when the current date is equal to the entered, until then the logic is simple but I’m having problems to apply it to a function.

I watched a video classroom but it is very old and uses parameters that I could not use so I adapted the code but still does not work.

The function is 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 that date is as datetime and in the HTML is in an input with the type datetime-local. Please help me urgently because I have to deliver a preview of this system tomorrow.

  • Welcome Julia, know that it is always good to mark an answer as you accept, in case she solves your problem. See how in https://i.stack.Imgur.com/evLUR.png and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

2 answers

0

Good night,

Do it this way and make sure it’s solved:

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

0


Assuming the connection link to the database is:

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

  1. Just use date_default_timezone_set('America/Sao_Paulo'); only once at the beginning of PHP.

  2. In querys (select and update) the link to the database connection is missing mysqli_query($conn,"SELECT... and mysqli_query($conn,"UPDATE

  3. Return the database date in the original format and set the dataAgora in the same format, that is to say $dataAgora = date('Y-m-d H:i:s');

  4. But 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 same-day dates with a time before the current time. The explanation? I do not know. I tested and found!

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.