Check last records per hour

Asked

Viewed 1,461 times

1

I’m having doubts in extracting data from the last hour of the mysql database, the code below makes the sum of values in the table for the last day, more relating for the last hour I’m not able to do.

   $busca = mysql_connect("$local","$usuario","$senha") or die("ERRO AO CONECTAR AO MYSQL, VERIFIQUE COM O ADMINISTRADOR" . mysql_error());
            mysql_select_db("$banco") or die("BASE DE DADOS INVÁLIDO");
            $pesquisa = mysql_query("SELECT sum(ProdValor) FROM vendas WHERE data BETWEEN CURRENT_DATE()-30 AND CURRENT_DATE()");
            while($sum = mysql_fetch_array($pesquisa)){
            $soma5 = $sum['sum(ProdValor)'];
            }
            //Mostrando o Resultado

            //$resultado = number_format($soma,2,",",".");

            echo $soma5;
  • What is the format of the field data?

  • @Sergio is in date format.

  • Experiment with WHERE data >= DATE_SUB(NOW(), INTERVAL 1 HOUR)

  • @Sergio updated to: mysql_query("SELECT sum(ProdValor) FROM vendas WHERE data >= DATE_SUB(NOW(), INTERVAL 1 HOUR)"); and no value is shown, even though there were records in the database 1 hour ago.

2 answers

1

For me what Sergio said it worked:

WHERE data >= DATE_SUB(NOW(), INTERVAL 1 HOUR)

Maybe it’s because in the field structure in your table you have put only DATE, place DATETIME you’re here because it worked for me.

0

There is that way too:

$data_hora_intervalo = 
    date('Y-m-d H:i:s', strtotime('-1 hour', strtotime(date('Y-m-d H:i:s'))));

where data>='$data_hora_intervalo'

Browser other questions tagged

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