Format JSON result with data returned every 30 minutes

Asked

Viewed 302 times

-3

  • 1

    And what’s the problem? What did you do and what error occurred?

  • 1

    Could you elaborate on your answer?

  • 1

    Now the meaning of the question has completely changed, from being a javascript problem to being a back-end PHP problem.

1 answer

0


You can try the following 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)


        $resultado[] = processa_query($data_hora ); // chama a função que irá processar o sql
    }
}

// monta o resultado em $JSON e exibe o mesmo, o implode junta a array em uma string
echo $JSON = '{ "dados":[' .  implode(', ', $resultado) . ']}';

function processa_query($data_hora){



    $sql = "SELECT valor FROM valores where data_hora < '$data_hora' order by id desc limit 1  ";
    $consulta = $pdo->query($sql);
    $resultado = $consulta->fetchAll(3);

    $valor_equipamento = $resultado[0][0];

    // através do explode, separo a data da hora (separados pelo espaço)
    $hora_minuto = explode(' ' ,$data_hora);
    // removo os segundos
    $hora_minuto = substr($hora_minuto[1], 0,5);
    // monto um item o JSON concatenando: 
    // label: recebe a variável $hora_minuto 
    // y: recebe o $valor_equipamento (sem aspas)
    // indexLabel: recebe o $valor_equipamento (com aspas)
    return "{label:'$hora_minuto', y:$valor_equipamento , indexLabel:'$valor_equipamento'}";
}
  • Duplicated of http://answall.com/a/148798/3635, if there was already an answer, then please do not duplicate it.

  • @Guilhermenascimento: Is it correct to edit the previous question to change the output result? That before were only date and time and now would be JSON?

  • The question has been marked as duplicate, if you think it is not then you should vote for reopening. Which may be quite correct since here you speak of JSON.

  • @Guilhermenascimento, I’m not questioning no... I just want to understand what would be best for the community... he should change the first question he wanted to know how to return the hours... and had already been answered and open another to format the output with JSON, or would it be better for him to edit the question?

  • I understood, I think you should keep both answers, but explain this one better and modify this question and vote to reopen, since here is another problem. The problem that the AP is who did not know how to ask, because he already had the answer, only the JSON part was missing, but he preferred to repeat the problem. So for the best benefit is to edit the question even. Of course this is my opinion :)

  • @Guilhermenascimento, thank you very much for the explanation. It was clear to me that I had spoken to the author of the question via the chat, but the question was really confused and seemed duplicated. I’ve already made the issue... but I don’t know yet or don’t have enough privilege to vote to close or reopen. Thank you very much for your attention.

  • I only recommend that you explain the current code, because it has the explanation of how you did the 24 hours, but it does not have the explanation of concatenation and implode, only a tip to enrich the answer ;) ... PS: I voted to reopen.

  • I reopened, but I think it is good that the author of the question is more attentive to explain the next doubts, because it seems to me a lack of clarification in the previous one. (in fact if it is already what is answered, it did not need to reopen, but finally...)

  • @Bacco, thank you ... it was my fault, because as I talked to him in chat and he is a user of low score, I should have advised him better. Serves as experience for the next.

  • @Guilhermenascimento, explanations added!

  • I added a small detail

Show 6 more comments

Browser other questions tagged

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