10
When I subtract the times the result is "0" I want it to be in the time format, example: 19:38:20 - 19:37:00 = 00:01:20.
Note: The values "time" of the database I am assigning to "$time1" and "$time2" are in varchar format. have already tested both and are in the correct format, but when subtracting is not in the time format as said before...
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"];
$sql = "DELETE FROM tempo WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
} else {
echo "Erro ao tentar deletar: " . $conn->error;
}
echo "$tempo1 <br>";
// segunda parte 2.2
$sql = "SELECT * FROM tempo2";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$id = $row["id"];
$corrida = $row["corrida"];
$nome1 = $row["nome"];
$numero = $row["numero"];
$tempo2 = $row["tempo"];
echo "$tempo2 <br>";
$sql = "DELETE FROM tempo2 WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
} else {
echo "Erro ao tentar deletar: " . $conn->error;
}
$tempo = $tempo2 - $tempo1;
echo "$tempo <br>";
}
}
PHP does not have native type for dates and times, so it doesn’t make much sense
$tempo = $tempo2 - $tempo1
. You need to convert to some suitable format before doing the operation. I would suggest you [Edit] the question by adding the type of variable that is being returned in $time1 and 2, so that it is easier to indicate a more suitable path for the solution.– Bacco
@Bacco I don’t know any other language with a native type for time, but in PHP we have the class
DateTime
to do all this there.– gmsantos
@gmsantos usually prefer to use simple functions for this type of case, I find very unnecessarily coiled the implementation of Datetime (one of the things I usually do to optimize PHP including is to remove any occurrence of Datetime that can be done with native time functions). While Datetime is instantiating the object, my "loose" line has already returned. Just out of curiosity, Clipper and Harbour have native date type. Harbour has native timestamp too. You can write this in Harbour:
d = 0d20160425
, for example. If you give aprint d + 6
will get01/05/2016
– Bacco