Problems showing duration in hours and minutes in PHP

Asked

Viewed 103 times

0

I’m trying to calculate a duration between column values in my database. The value until it is being calculated correctly, but it turns out that the hour:minute format is not going out as expected.

Assuming that in my database I have the first duration that is 1:30 and the second duration that is 2:30, will total 4:00 hours, but what comes out to me on screen is 4:0 of this format, how can I solve?

Follow below part of my code that makes the operation:

$query = mysql_query("SELECT ass.Duracao, pa.horas_dadas, pa.Horas_restantes, pa.Qtd_horas, ass.ID_Aluno, ass.ID_Pacote, ass.ID_Aula, pa.id_alunos FROM Assiste ass JOIN pacotes pa ON pa.ID=ass.ID_Pacote WHERE ID_Aluno = $pegaid AND ID_Pacote = $pegaid_pacote AND Status_presence = 'Ativa'"); 

while($row = mysql_fetch_array($query)) 
{ 
    $hora= $row['Duracao']; 
    $show_qtd_horas = $row['Qtd_horas']; 
    $hora1 = explode(":",$hora); 
    $min_hora1 = ($hora1[0] * 3600) + ($hora1[1] * 60); 
    $total=$total+$min_hora1; 
} 

$hora = floor($total / 3600); 
$total = $total - ($hora * 3600); 
$min = floor($total / 60)/10; 
$min=str_replace(".","",$min);
$result_duracao=$hora.":".$min; 

if(strlen($min)==1)
{ 
    $min=$min."0"; 
} 

$update_pacotes = mysql_query("UPDATE pacotes SET horas_dadas='$result_duracao' WHERE id_alunos=$pegaid AND ID=$pegaid_pacote");

echo $result_duracao;
  • How is the time saved in the Duration column? already in some format? H:mm || seconds?

2 answers

1


   while($row = mysql_fetch_array($query)) 
   { 
     $hora= $row['Duracao']; 
     $show_qtd_horas = $row['Qtd_horas']; 
     $hora1 = explode(":",$hora); 

     $min_hora1 = ($hora1[0] * 3600) + ($hora1[1] * 60); 
     $total=$total+$min_hora1; 
   } 
     $hora = floor($total / 3600); 
     $total = $total - ($hora * 3600); 
     $min = floor($total / 60)/10; 
     $min=str_replace(".","",$min);
     $result_duracao=$hora.":".$min; 
     if(strlen($min)==1){ 
        $result_duracao=$result_duracao."0"; 
     } 

      $testeHora = explode(":",$result_duracao);
      $lenHora= strlen($testeHora[0]);

      if($lenHora==1){ 
        $result_duracao="0".$result_duracao; 
      }

1: In your code change $min=$min."0"; for $result_duracao=$result_duracao."0";

2: add these lines at the end of the code before the update.

 $testeHora = explode(":",$result_duracao);
 $lenHora= strlen($testeHora[0]);
    if($lenHora==1){ 
      $result_duracao="0".$result_duracao; 
    }

which will check if the part of the hour contains 1 or 2 digits. If you have a digit you will concatenate with a 0 in front.

  • Your reply was the best, thank you very much

0

If the calculation is correct you can use a native php 'date' function to display in the correct pattern.

echo date('H:i', strtotime($nomedavariavel));

see example using your code in phpFiddle

  • Can’t because I need to send the data to the database

  • not to display on screen, need this data to be updated in the bank

  • Saves a variable with the result example: $variavel_bd = date('H:m', strtotime($result_duration));

  • Thank you, everything settled here

Browser other questions tagged

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