- Is there any function I pass minutes and returns hours and minutes in PHP?
I have tried mktime function but not working more than 24 hours see bellow example
echo date('H:i', mktime(0,257));
Asked
Active
Viewed 2,351 times
0

Parag Tyagi
- 8,780
- 3
- 42
- 47
2 Answers
2
Try this one
function hour_min($minutes){// Total
if($minutes <= 0) return '00 Hours 00 Minutes';
else
return sprintf("%02d",floor($minutes / 60)).' Hours '.sprintf("%02d",str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT)). " Minutes";
}
echo hour_min(500);
this will return output : 08:20

HIR
- 192
- 3
- 11
-
Explaining your code will be more helpful to researchers that don't follow _why_ you recommend this approach. Please understand that researchers don't always know what all of these native functions and arithmetic operators do and you can speed up their understanding by adding explanation. Yes, they can make 4 trips to the PHP manual, but if you add _some_ explanation, you stand a better chance of empowering researchers (instead of them saying "oh well, I don't understand it, but it works -- I'll just copy-paste it". – mickmackusa Aug 31 '21 at 07:12
1
You can use this:
$hours = floor($minutes / 60).':'.str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT);

krasipenkov
- 2,031
- 1
- 11
- 13