Why isn’t the time difference returning?

Asked

Viewed 53 times

0

I asked the same question, but I want to know where the error is, because 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’s not the result. You’re 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>
  • Hello, can you organize your code please. (I suggest you remove the commented code to facilitate reading)

  • You yourself said you have already asked this question, you could have asked in the other. Avoid creating duplicates.

  • 1

    When choosing the simplest code it is easier to hit or find the error. In the original question he had the simple solution, but he chose the complicated one. http://answall.com/q/125086/101

  • If you are talking about this "$tempo = date('H:i:s', strtotime( $tempo1 ) - strtotime( $tempo2 ) ;" this only subtracts the time, but does not show the time difference example: 8:50:10 - 8:50:00 = 23:59:50. example of how I want: 8:50:10 - 8:50:00 = 00:00:10

  • 1

    @Vanpersie I think you didn’t see the demo, which shows exactly what you asked for in the question, has link there showing working. Clearly you’re reversing the values on your test, but if you had commented, I would simply complement it with a abs( ) to function independently of the order. The fact is that you just need to click on the IDEONE link to see the code working, but if you want the inverted values to work as well, just one $tempo = date('H:i:s', abs( strtotime( $tempo1 ) - strtotime( $tempo2 ) ) )

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 it out of while, because it’s just a function and Voce doesn’t need multiple functions but needs to use/invoke several times.

After placing the function outside of while (and before while) you have to call/invoke the function where you want it to be used.

In your example will be.:

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.

  • Fatal error: Uncaught Exception 'Exception' with message 'Datetime::__Construct(): It is not safe to rely on the system’s Timezone Settings. You are required to use the date.Timezone Setting or the date_default_timezone_set() Function. In case you used any of those methods and you are still Getting this Warning, you Most likely misspelled the Timezone Identifier. We Selected the Timezone 'UTC' for now, but Please set date.Timezone to select your Timezone... knows how to tell me the cause of the error?

  • For what I noticed you have not configured Timezone in php.ini.see if it helps.: http://php.net/manual/en/timezones.php

Browser other questions tagged

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