why are not you returning the time difference? [duplicate]

0

I asked the same question, but I want to know where the error is, since it does not return the time difference.

obs: I want you to show the time difference. example: 7:50:10 - 7:50:00 = 00:00:10, but that was not the result. It is not returning anything.

code:

<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <?php
include "../../conexao.php";
// primeira parte 1
$sql = "SELECT * FROM tempo";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
    $id = $row["id"];
    $tempo1 = $row["tempo"];
    echo "$tempo1 <br>";
    $sql = "DELETE FROM tempo WHERE id='$id'";
    if ($conn->query($sql) === TRUE) {
    } else {
        echo "Erro ao tentar deletar: " . $conn->error;
    }
    // segunda parte 2.2
    $sql = "SELECT * FROM tempo2";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
        $id1 = $row["id"];
        $corrida = $row["corrida"];
        $nome1 = $row["nome"];
        $numero = $row["numero"];
        $tempo2 = $row["tempo"];
        echo "$tempo2 <br>"; 
        $sql = "DELETE FROM tempo2 WHERE id='$id1'";
        if ($conn->query($sql) === TRUE) {
        } else {
            echo "Erro ao tentar deletar: " . $conn->error;
        }
        function dateDiff( $tempo1, $tempo2, $format = '%H:%i:%s' ) {
            $d1     =   new DateTime( $tempo1 );
            $d2     =   new DateTime( $tempo2 );
            //Calcula a diferença entre as datas
            $diff   =   $d1->diff($d2, true);   
            //Formata no padrão esperado e retorna
            return $diff->format( $format );
        }
    }
}
        ?>
    </body>
</html>
    
asked by anonymous 25.04.2016 / 12:58

1 answer

1
    function dateDiff( $tempo1, $tempo2, $format = '%H:%i:%s' ) {
        $d1     =   new DateTime( $tempo1 );
        $d2     =   new DateTime( $tempo2 );
        //Calcula a diferença entre as datas
        $diff   =   $d1->diff($d2, true);   
        //Formata no padrão esperado e retorna
        return $diff->format( $format );
    }

Put out of the while, because it is just a function and you do not need several functions but need to use / invoke several times.

After putting the function out of the while (and before the while) you must call / invoke the function where you want it to be used.

Your example will look like this:

                                                    

function dateDiff( $tempo1, $tempo2, $format = '%H:%i:%s' ) {
    $d1     =   new DateTime( $tempo1 );
    $d2     =   new DateTime( $tempo2 );
    //Calcula a diferença entre as datas
    $diff   =   $d1->diff($d2, true);   
    //Formata no padrão esperado e retorna
    return $diff->format( $format );
}

// primeira parte 1
$sql = "SELECT * FROM tempo";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
    $id = $row["id"];
    $tempo1 = $row["tempo"];
    echo "$tempo1 <br>";
    $sql = "DELETE FROM tempo WHERE id='$id'";
    if ($conn->query($sql) === TRUE) {
    } else {
        echo "Erro ao tentar deletar: " . $conn->error;
    }
    // segunda parte 2.2
    $sql = "SELECT * FROM tempo2";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
        $id1 = $row["id"];
        $corrida = $row["corrida"];
        $nome1 = $row["nome"];
        $numero = $row["numero"];
        $tempo2 = $row["tempo"];
        echo "$tempo2 <br>"; 
        $sql = "DELETE FROM tempo2 WHERE id='$id1'";
        if ($conn->query($sql) === TRUE) {
        } else {
            echo "Erro ao tentar deletar: " . $conn->error;
        }
        echo dateDiff($tempo1, $tempo2, $format = '%H:%i:%s').'<br>';
    }
}
        ?>
    </body>
</html>

I put a 'br' for the information to be separated by line.

    
25.04.2016 / 13:41