Datediff calculating only time

Asked

Viewed 57 times

1

I currently have a code that calculates the difference of 2 date/time but my calculation is only calculating the time and if the final date is another day the time does not count +24h and only makes the difference of the time as if it were the same day. How could I make to calculate the whole period only time? example:

Dtinitial-> 01/10/2019 15:00

Dtfinal -> 02/10/2019 10:00

Expected result -> 19:00

Result obtained (wrong) -> 5:00

$resultado_4 = mysql_query($query_4, $conexao);
                      while($dados_4 = mysql_fetch_array($resultado_4))
                      {

                        $id_parada = $dados_4 ['id'];  
                        $id_origem_problema =  $dados_4['origem_problema'];
                        $id_classificacao =  $dados_4['classificacao'];
                        $id_tecnologia =  $dados_4['tecnologia'];
                        $id_causa =  $dados_4['causa'];
                        $identificacao = $dados_4['identificacao'];
                        $id_departamentoproblema = $dados_4['departamento'];
                        $contato_local = $dados_4['contato_local'];
                        $sla_operadora = $dados_4['sla'];
                        $data_abertura = $dados_4['data_abertura'];
                        $data_fechamento = $dados_4['data_fechamento'];
                        $id_atendente = $dados_4['usuario'];
                        $contato_local = $dados_4['contato_local'];

                        $dt1 = new DateTime($data_abertura);
                        $dt2 = new DateTime($data_fechamento);

                        $dtAbertura = new DateTime($data_abertura);
                        $dtFechamento = new DateTime($data_fechamento);


                  ?>
                      <tr>
                          <td id = "CC"> <?php echo $id_origem_problema; ?> </td>
                          <td id = "CV"> <?php echo $id_classificacao; ?> </td>
                          <td id = "CV"> <?php echo $id_tecnologia; ?> </td>
                          <td id = "CV"> <?php echo $id_causa; ?> </td>
                          <td id = "CV"> <?php echo $identificacao; ?> </td> 
                          <td id = "CV"> <?php echo $id_departamentoproblema; ?> </td> 

                          <td id = "CV"> <?php echo date_format($dtAbertura, 'd/m/Y H:i'); ?> </td> 
                          <td id = "CV"> <?php echo date_format($dtFechamento, 'd/m/Y H:i'); ?> </td> 
                          <td id = "CV"> <?php echo $contato_local; ?> </td> 

                          <td id = "CV"> <?php echo $sla_operadora; ?> </td> 
                          <?php
                          $dteDiff  = $dt1->diff($dt2);
                          $dataFormatada = $dteDiff->format("%d %H:%I:%S"); ?>
                          <td id = "CV" style="color: <?php if($dataFormatada <= $sla_operadora) { ?> blue <?php } else { ?> red <?php } ?>  "  > <?php
                            print $dataFormatada; ?> </td>
                          <td id = "CV"> <?php echo $id_atendente; ?> </td> 



                      </tr>
                  <?php
                      }     
                  ?>
  • UPDATE: problem solved via database.. just bring up a column using TIMEDIFF( startData, startData) function by mysql

1 answer

3

It’s a date that comes from a database so it should already be in the correct format, so do it:

$dt1 = date_create('2019-10-01 15:00');
$dt2 = date_create('2019-10-02 10:00');

and with method diff and format:

var_dump($dt1->diff($dt2)->format("%d %H:%I:%S"));

with the following result:

string(10) "0 19:00:00"

maybe it solves your initial problem, but, I’m not sure what might help you because the return of a method diff is an object Dateinterval.

Example of a class object Dateinterval:

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 0
)

Online Example

To calculate days to hours do:

$diff = $dt1->diff($dt2);
$horas = $diff->h + ($diff->days * 24);
echo "{$horas}  horas e $diff->i minutos";

Online Example

  • the answer was useful but not quite what I need.. I actually wanted to calculate everything in hours and minutes... for example: difference of 2 days.. would bring me 48 hours. and not 2 00:00:00

  • My friend @Fernandofefu boy the Dateinterval gives you everything you need, you talking like that seems that the answer does not aim to timing ... and has .. it gives you the difference in time (in any format just need to do)

  • If you need to transform days into hours multiply by 24 understood? @Fernandofefu I made an example take a look...

Browser other questions tagged

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