convert minutes to : hours and minutes

Asked

Viewed 264 times

0

Have 150 minutos; how to convert it to stay 2h30min?
For example, I was able to do a conversion using the code below, but it will return 2.55 where’s the mistake ?

$minutos = 150;

if($minutos > 60){
    $exibe2 = $minutos / 60;
    $exibe = $exibe2 . "min"; 
}else{
    $exibe = "0:" . $minutos;
}
$hora_final = $exibe;
  • in this example you sent, happened what I quoted here @Sam

2 answers

1

You are doing a decimal division and PHP is returning correctly. What you should do is use the hours only the whole part and do the rest operation to catch the minutes:

$horas = (int) $minutos / 60;
$min = $minutos % 60; 
$exibe = "{$horas}h{$min}min";

1

$minutos = 150;

$horas = floor($minutos / 60);
$minutos = $minutos % 60;
$exibe = $horas."h ".$minutos."m";
$hora_final = $exibe;

Browser other questions tagged

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