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)
– Tiago Gomes
You yourself said you have already asked this question, you could have asked in the other. Avoid creating duplicates.
– user28595
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
– Maniero
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
– Van Persie
@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 ) ) )
– Bacco