Subtraction of hours in php

Asked

Viewed 10,575 times

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>";

}

}
  • 2

    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.

  • 1

    @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.

  • 3

    @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 a print d + 6 will get 01/05/2016

5 answers

14


You can use the Datetime() class to find out the difference between two dates.

/**
 * @param $dateStart - Data inicial
 * @param $dateEnd - Data final
 * @param $format  - Formato esperado de saida
 */
function dateDiff( $dateStart, $dateEnd, $format = '%a' ) {

    $d1     =   new DateTime( $dateStart );

    $d2     =   new DateTime( $dateEnd );

    //Calcula a diferença entre as datas
    $diff   =   $d1->diff($d2, true);   

    //Formata no padrão esperado e retorna
    return $diff->format( $format );

}

The types of output available are: %Y Years, %m Months, %d Days, %H Hours, %i Minutes, %s Seconds. For your case, Voce will pass in the last parameter the following string: '%H:%i:%s'

I hope it helps.

  • Parse error: syntax error, Unexpected 'public' (T_PUBLIC) in C: Users Kaio Gabriel Desktop finderserver root Finder timekeeping Calc bd_result.php on line 55. can tell me the cause?

  • remove the public of the method statement. I will update the response

  • 1

    seven as best answer please

  • 2

    @Vanpersie See the [tour] you choose the answer that suits you best, you don’t have to choose the one you requested.

  • 3

    @It’s nice to be asked to accept your answer. If it is to instruct the new user, let it be impartial giving the chance to all be accepted.

  • 1

    @bigown, he had already accepted my answer, but he had not set it as the best answer. There’s one guy who forgets that.

Show 1 more comment

13

Note: I didn’t remember this one and ended up answering a duplicate with a solution even more elegant than the one here (and obviously without using Datetime, which is more important).

For those who will pass, follow link:

/a/149561/70



In this case, how do they treat strings, a PHP line with ordinary functions solves:

$tempo = gmdate('H:i:s', strtotime( $tempo1 ) - strtotime( $tempo2 ) );

See working on IDEONE.

The function strtotime() in this context will transform time into numerical (in seconds), allowing conventional mathematical operations.

Then the function of gmdate converts into string again, in the format you put in the question.

Of curiosity, if you only use this part:

strtotime( $tempo1 ) - strtotime( $tempo2 )

the result will be 80, which is the number of seconds between the times of your example.

As the time base of strtotime() is the Unix epoch, you can do this to convert a time to "pure" seconds (without the date part):

strtotime( '1970-01-01 '.$horario );

As commented on another OP duplicate, follows an alternative to use inverted values:

$tempo = gmdate('H:i:s', abs( strtotime( $tempo1 ) - strtotime( $tempo2 ) ) )

It’s actually an example that I didn’t think was necessary, because it’s extremely basic, but there it is.

See working on IDEONE.

7

You can also use a library called Wallacemaxters Timer to do this:

use WallaceMaxters\Timer\Time;

$t1 = Time::createFromFormat(Time::DEFAULT_FORMAT, $tempo1);

$t2 = Time::createFromFormat(Time::DEFAULT_FORMAT, $tempo2);

$tempo = $t1->diff($t2)->format(Time::DEFAULT_FORMAT);

In the source code of this library, we can see how the values are compared:

public function diff(Time $time, $absolute = true)
{
    $diff = $this->getSeconds() - $time->getSeconds();
    return new self(0, 0, $absolute ? abs($diff) : $diff);
}

I created this library to be able to work with time in PHP, especially those that exceed 24 hours.

Important reading:

How to get the format in hours when it exceeds 24?

1

In a very simple way, you can do the following:

<?php
$dteini   = "2018/08/01 12:00:00";
$dtefim   = "2018/08/03 12:59:19";
$dteDiff  = strtotime($dtefim) - strtotime($dteini);
$dte      = new datetime(date('H:i:s'));
$dte      = $dteDiff;
print "Total " . $dte . "<br>";
$total    = $dte;
$horas    = floor($total / 3600);
$minutos  = floor(($total - ($horas * 3600)) / 60);
$segundos = floor($total % 60);
$horas    = str_pad($horas, 2, "0", STR_PAD_LEFT);
$minutos  = str_pad($minutos, 2, "0", STR_PAD_LEFT);
$segundos = str_pad($segundos, 2, "0", STR_PAD_LEFT);
echo $horas . ":" . $minutos . ":" . $segundos;
?>

Upshot:

Total 176359
48:59:19

1

Maybe I’ll help!

<?php

date_default_timezone_set('America/Sao_Paulo');

// Data e hora atual
$datetime1 = date("Y-m-d H:i:s");

// Converter $datetime1 para o formato Unix timestamp
$timestamp = strtotime($datetime1);

// Tempo a ser subtraido - 01:30 horas
$horas = 1;      // cada hora corresponde a 3600 segundos
$minutos = 30;   // cada minuto corresponde a 60 segundos
$segundos = 0;

// Transforma o tempo a ser subtraido em segundos
$tempo = ($horas * 3600) + ($minutos * 60) + $segundos;

// Subtrair $tempo de $datetime1
$novaDataHora= $timestamp - $tempo;

// Data e hora apos a subtracao
$datetime2 = date("Y-m-d H:i:s", $novaDataHora);

// Mostra os resultados
echo $datetime1 . '<br>';
echo $datetime2 . '<br>';

?>

Browser other questions tagged

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