Rob Rob - 1 month ago 9
PHP Question

Convert number of minutes into hours & minutes using PHP

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 17 minutes

Answer
<?php

function convertToHoursMins($time, $format = '%02d:%02d') {
    if ($time < 1) {
        return;
    }
    $hours = floor($time / 60);
    $minutes = ($time % 60);
    return sprintf($format, $hours, $minutes);
}

echo convertToHoursMins(250, '%02d hours %02d minutes'); // should output 4 hours 17 minutes