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 :

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


  1. André Medeiros

    Said on March 18, 2008 :

    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. André Medeiros

    Said on March 19, 2008 :

    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. Evan Wired

    Said on March 26, 2008 :

    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. Admin

    Said on March 26, 2008 :

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

  5. Evan Wired

    Said on March 26, 2008 :

    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. stratos

    Said on June 2, 2008 :

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

  7. Dennis

    Said on July 5, 2008 :

    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;
    }

  8. Warz

    Said on October 11, 2008 :

    mmh you could use mktime
    $sec = 500;
    date(‘H:i:s’,mktime(0,0,$sec,0,0,0));

  9. André Medeiros

    Said on October 14, 2008 :

    @Wars

    Creative use of the mktime function. Right on! :)

  10. Luke H

    Said on April 6, 2009 :

    Warz. that one line use of mktime to do this is just gorgeous. Thanks!

  11. Francis

    Said on August 2, 2009 :

    I’ve changed the initial function so it displays days too.

    function ActiveMinutesToHours($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) 24)
    {
    $iDays = $iHours/24;
    if($iDays == 1)
    $iDays .= ” day, “;
    else
    $iDays .= ” days, “;
    }
    $tHours = $iDays . $iHours .” hours and “. $Minutes . “minutes.”;
    return $tHours;
    }

  12. Ozma

    Said on January 19, 2010 :

    Cheers, worked like a dream <3

  13. RD

    Said on January 23, 2010 :

    Thanks everyone! You made it so easy…

  14. omdb

    Said on April 12, 2010 :

    I guess we don’t need a fancy function with Warz quality suggestion of mktime.

    Just multiply minutes with 60 and use
    $sec = 500;
    date(‘H:i:s’,mktime(0,0,$sec,0,0,0));

    That’s all you need to do. (‘H:i:s’) can be changed to whatever format you like (‘H \h\r\s i \m\i\n\s s \s\e\c\o\n\d\s’) will print “xx hrs xx mins xx seconds” :)

    Thanks for this nice page and the codes you guys share. :)

  15. someonehere

    Said on June 10, 2010 :

    Hello
    Please, complete newbie question, but I have a page that takes the information in minutes from MySQL and displays it in hours. The problem is that I’d like for it not to display either the hours or the minutes when they equal 0, for example, currently, when something is 120 minutes, it displays 2h 0m , and when it’s 45 minutes it displays 0h 45m. Is there any way those could display simply as 2h or as 45m? The code I am using is below:

    if ($row[“runtimem”]””){
    $runh = floor($row[“runtimem”] / 60);
    $runm = $row[“runtimem”] % 60;
    echo $runh . “h ” . $runm . “m   “;
    }

    Please, any ideas would be very appreciated.

  16. bazdgs

    Said on April 4, 2011 :

    $minuts = “your minuts here”;
    echo floor($minuts/60).” : “.($minuts%60);

  17. Trivikram

    Said on May 19, 2011 :

    Hi,
    I woriking on some attendence management, where I calculate total work hours and average work hours for a week,month etc. I want average work hours not to be as 9.21 but 9 Hours 10Minutes like that. Is that Possible ??? If yes, May I know How ???

  18. Johannes

    Said on June 10, 2011 :

    Can use this if you want. Will show days and time as required. based on info from these posts.

    //======= CODE ====

    $baseTime= mktime(0,$minute,0,0,0,0);
    $days= round(abs($baseTime-mktime(0,0,0,0,0,0))/60/60/24);
    $hours= date(“H:i”,$baseTime);
    if ($days> 0) {
    return “$days days $hours”;
    } else {
    return $hours;
    }

    //======= END ===

    Hope it helps.

1 Trackback(s)

  1. Jan 31, 2010: PHP – Online Time Card Application « Blog on the WWW

Post a Comment