-3

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 .

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
martin
  • 1
  • 2

3 Answers3

3

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
Patrick Janser
  • 3,318
  • 1
  • 16
  • 18
2

It appears that you have one variable that stores an amount of minutes, $cat['prepration_time'] in your case. To output the minutes as decimal hours all you really need to do is to divide the number of minutes by 60, that's the number of minutes in one hour. In your case that would be

echo $cat['preparation_time'] / 60 . ' hours';

To avoid awkward results like 1.33333333333 hours, you can additionally wrap the calculation in round, where the second parameter of round is the precision, i.e. the number of digits behind the dot.

$minutes = 77;
echo round($minutes / 60, 2) . ' hours'; // 1.28 hours

Be aware however that this format is badly readable and prone to misunderstanding. Does 1.30 hours mean 1 hour and 30 minutes or rather 1 hour and 18 minutes? And while you might be able to convert 1.30 hours in your head, you will surely need a calculator for 1.283 hours. I would recommend to use a commonly understood and unambiguous format, like for example 1 hour and 32 minutes, or 1:32 hours.

Christoph
  • 524
  • 10
  • 19
-1

Add this code.

Firstly add the hour and minutes after that I have added round function so it will show proper output upto 2 decimal.

$hours =1; $minutes=30 ; $CalculateHours = $hours + round($minutes / 60, 2); echo $CalculateHours . " hours";

Result: 1.5 Hours