time - PHP: display 90 min as 1.5 hours
Get the solution ↓↓↓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 .
Answer
Solution:
I agree with all the comments saying that it will be clearer to display1h20
than1.33 hours
. You may could round it and display1 h and ВЅ
or1 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
Answer
Solution:
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 like1.33333333333 hours
, you can additionally wrap the calculation in round, where the second parameter ofround
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. Does1.30 hours
mean1 hour and 30 minutes
or rather1 hour and 18 minutes
? And while you might be able to convert1.30 hours
in your head, you will surely need a calculator for1.283 hours
. I would recommend to use a commonly understood and unambiguous format, like for example1 hour and 32 minutes
, or1:32 hours
.
Answer
Solution:
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
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: dompdf image not found or type unknown
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.