It is simpler with loop
for time and another for minutes.
I've separated a part of the code into a function to make it easier to understand the code:
for($hora= 0; $hora < 24; $hora++){
$hoje = new DateTime();
for($minuto = 0; $minuto <= 30; $minuto += 30){
$data_hora = $hoje->format('Y-m-d') . ' ' // dia mes e ano de hoje
. str_pad($hora, 2,'0', STR_PAD_LEFT) // hora do primeiro loop (formatado com zero à esquerda)
. ':' . str_pad($minuto, 2,'0', STR_PAD_LEFT) . ':00'; // minutos do segundo loop (formatado com zero à esquerda)
// nota: os segundos estão fixos em 00 (linha acima)
processa_query($data_hora ); // chama a função que irá processar o sql
}
}
function processa_query($data_hora){
$sql = "SELECT * FROM valores where data_hora < '$data_hora' order by id desc limit 1 ";
$consulta = $pdo->query($sql);
$resultado = $consulta->fetchAll();
foreach ($resultado as $row) {
$nome_equipamento = $row['nome'];
$valor_equipamento = $row['valor'];
$data_alarme = $row['data_hora'];
$data_a = date("H", strtotime($data_alarme));
if ($data_a != $conta) {
$valor_equipamento = 0;
}
$data_alarme = date("d/m/Y - H:i:s", strtotime($data_alarme));
}
}
The processa_query
function will receive the current date and the variation every 30 minutes as a parameter. Example of parameters passed on 08/24/2016:
2016-08-24 00:00:00
2016-08-24 00:30:00
2016-08-24 01:00:00
2016-08-24 01:30:00
2016-08-24 02:00:00
2016-08-24 02:30:00
2016-08-24 03:00:00
2016-08-24 03:30:00
2016-08-24 04:00:00
2016-08-24 04:30:00
2016-08-24 05:00:00
2016-08-24 05:30:00
2016-08-24 06:00:00
2016-08-24 06:30:00
2016-08-24 07:00:00
2016-08-24 07:30:00
2016-08-24 08:00:00
2016-08-24 08:30:00
2016-08-24 09:00:00
2016-08-24 09:30:00
2016-08-24 10:00:00
2016-08-24 10:30:00
2016-08-24 11:00:00
2016-08-24 11:30:00
2016-08-24 12:00:00
2016-08-24 12:30:00
2016-08-24 13:00:00
2016-08-24 13:30:00
2016-08-24 14:00:00
2016-08-24 14:30:00
2016-08-24 15:00:00
2016-08-24 15:30:00
2016-08-24 16:00:00
2016-08-24 16:30:00
2016-08-24 17:00:00
2016-08-24 17:30:00
2016-08-24 18:00:00
2016-08-24 18:30:00
2016-08-24 19:00:00
2016-08-24 19:30:00
2016-08-24 20:00:00
2016-08-24 20:30:00
2016-08-24 21:00:00
2016-08-24 21:30:00
2016-08-24 22:00:00
2016-08-24 22:30:00
2016-08-24 23:00:00
2016-08-24 23:30:00
If I misunderstand your question, just post a comment below that I update.