Mar
18th | 2008

How to convert Minutes to Hours With PHP

Filed under PHP | Posted by Amit

I know that when you read the title you said “What is he talking about? Its easy”. Well it turns out that its not that easy. It took me a while to collect and create this, but here it is: a PHP code that converts minutes to hours.

<?php function ConvertMinutes2Hours($Minutes)
{
    if ($Minutes < 0)
    {
        $Min = Abs($Minutes);
    }
    else
    {
        $Min = $Minutes;
    }
    $iHours = Floor($Min / 60);
    $Minutes = ($Min - ($iHours * 60)) / 100;
    $tHours = $iHours + $Minutes;
    if ($Minutes < 0)
    {
        $tHours = $tHours * (-1);
    }
    $aHours = explode(“.”, $tHours);
    $iHours = $aHours[0];
    if (empty($aHours[1]))
    {
        $aHours[1] = “00″;
    }
    $Minutes = $aHours[1];
    if (strlen($Minutes) < 2)
    {
        $Minutes = $Minutes .“0″;
    }
    $tHours = $iHours .“:”. $Minutes;
    return $tHours;
}
//Test Code
echo ConvertMinutes2Hours(220); // This should print  3:40
?>

Hope this helps someone :)

Amit.

Tags: , , , ,

7 Responses to “How to convert Minutes to Hours With PHP”



  1. By André Medeiros on Mar 18, 2008 | Reply

    You can do it in far less lines.

    function convertMinutes2Hours($minutes) {
    $minutes = round($minutes); // assume full minutes
    $hours = floor($minutes / 60);
    $minutes = $minutes - ($hours * 60);
    return str_repeat(’0′, 2 - strlen($hours)) . $hours . ‘:’ . str_repeat(’0′, 2 - strlen($minutes)) . $minutes;
    }

  2. By André Medeiros on Mar 19, 2008 | Reply

    One-liner (if you’re not planning on using more than 24 hours):

    function convertMinutes2Hours($minutes) { return date(”G:i”, abs(round((int)$minutes))*60); }

  3. By Evan Wired on Mar 26, 2008 | Reply

    If $min is an integer, it is even simpler:

    $hours = sprintf(”%d:%02d”, abs((int)($min/60)), abs((int)($min%60)));

    The original code is a DailyWTF candidate.

  4. By Admin on Mar 26, 2008 | Reply

    Thanks for all the comments.
    I like your ideas and i will definitely use them

  5. By Evan Wired on Mar 26, 2008 | Reply

    Don’t use what I posted. Got too silly with the parens. Lesson: Always take more than 10 seconds to write your code :)

    $hours = sprintf(”%d:%02d”, abs((int)$min/60), abs((int)$min%60));

  6. By stratos on Jun 2, 2008 | Reply

    that str_repeat function is nice :) but i think date is the most appropriate function i guess…

  7. By Dennis on Jul 5, 2008 | Reply

    Sweet, thanks all. I’ve converted this to a tad longer, but more usable, returned text.

    function ConvertMinutes2Hours($Min) {
    $iHours = Floor($Min / 60);
    $Minutes = ($Min - ($iHours * 60)) / 100;
    $tHours = $iHours + $Minutes;
    if ($Minutes < 0){
    $tHours = $tHours * (-1);
    }
    $aHours = explode(”.”, $tHours);
    $iHours = $aHours[0];
    if (empty($aHours[1])) {
    $aHours[1] = “00″;
    }
    $Minutes = $aHours[1];
    if (strlen($Minutes) < 2) {
    $Minutes = $Minutes .”0″;
    }
    if ($iHours==0) {$tHours = $Minutes.’ minutes’;}
    else if ($iHours==1) {$tHours = $iHours.” hour and “.$Minutes.’ minutes’;}
    else {$tHours = $iHours.” hours and “.$Minutes.’ minutes’;}
    return $tHours;
    }

Post a Comment

Search Dev102