convert minutes to: hours and minutes [duplicate]

this question already has answers here : How to convert seconds to "hour:minute:second" format? (3 responses) Closed 1 year ago.

I have 150 minutos; How to convert this to stay 2h30min?
For example, I managed to make a conversion using the code below, but it returns 2.55 where ta the error ?

$minutos = 150;

if($minutos > 60){
    $exibe2 = $minutos / 60;
    $exibe = $exibe2 . "min"; 
}else{
    $exibe = "0:" . $minutos;
}
$hora_final = $exibe;
 0
Author: rLinhares, 2019-09-06

2 answers

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 take the minutes:

$horas = (int) $minutos / 60;
$min = $minutos % 60; 
$exibe = "{$horas}h{$min}min";
 1
Author: JĂșlio Neto, 2019-09-06 15:40:03
$minutos = 150;

$horas = floor($minutos / 60);
$minutos = $minutos % 60;
$exibe = $horas."h ".$minutos."m";
$hora_final = $exibe;
 1
Author: thxmxx, 2019-09-06 15:41:17