I have a variable called $final_time_saving
which is just a number of minutes, 250 for example.
How can I convert that number of minutes into hours and minutes using PHP in this format:
4 hours 10 minutes
I have a variable called $final_time_saving
which is just a number of minutes, 250 for example.
How can I convert that number of minutes into hours and minutes using PHP in this format:
4 hours 10 minutes
The easiest way is :
Sorry for bringing up an old topic, but I used some code from one of these answers a lot, and today I told myself I could do it without stealing someone's code. I was surprised how easy it was. What I wanted is 510 minutes to be return as 08:30, so this is what the code does.
I use short variable names because I'm going to use the function a lot, and I'm lazy.
@Martin Bean's answer is perfectly correct but in my point of view it needs some refactoring to fit what a regular user would expect from a website (web system).
I think that when minutes are below 10 a leading zero must be added.
ex: 10:01, not 10:1
I changed code to accept
$time = 0
since 0:00 is better than 24:00.One more thing - there is no case when
$time
is bigger than 1439 - which is 23:59 and next value is simply 0:00.Just in case you want to something like:
Thanks to @Martin_Bean and @Mihail Velikov answers. I just took their answer snippet and added some modifications to check,
If only Hours only available and minutes value empty, then it will display only hours.
Same if only Minutes only available and hours value empty, then it will display only minutes.
If minutes = 60, then it will display as 1 hour. Same if minute = 1, the output will be 1 minute.
Changes and edits are welcomed. Thanks. Here is the code.
function convertToHoursMins($time) {