Search code examples
phptimetime-format

PHP: display 90 min as 1.5 hours


Minutes=90 Need to display in hours

echo intdiv($ct['time'], 60).'.'. ($ct['time'] % 60)." Hours";

The output is 1:30 hours.

But i need to display as 1.5 hours. How to convert the minute into hours? As 1.5, 1.6, 1.7, 1.8, 1.9, 2 hours and so on .


Solution

  • I agree with all the comments saying that it will be clearer to display 1h20 than 1.33 hours. You may could round it and display 1 h and ½ or 1 hour and ¼ but still, it's not easy to read.

    If you want to stick to your hours then you could do this:

    $min = $cat['prepration_time'];
    if ($min < 60) {
        echo $min == 1 ? "$min minute" : "$min minutes";
    }
    else {
        $hours = round($min / 60, 1);
        echo $hours == 1 ? "$hours hour" : "$hours hours";
    }
    

    Examples of output:

    1 minute
    20 minutes
    50 minutes
    1 hour
    1.2 hours
    1.5 hours
    1.7 hours