Directory Freebies VS CheatSheet Forum

RSS

Email

Translate

Home About Archive Privacy Contact Advertise Write for Dev102
Posted by Amit on Mar 18th, 2008 | Filed under PHP |

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: , , , ,

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


  1. André Medeiros Said on Mar 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 Mar 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 Mar 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 Mar 26, 2008 :

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

  5. Evan Wired Said on Mar 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 Jun 2, 2008 :

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

  7. Dennis Said on Jul 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 Oct 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 Oct 14, 2008 :

    @Wars

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

  10. Luke H Said on Apr 6, 2009 :

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

  11. Francis Said on Aug 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 Jan 19, 2010 :

    Cheers, worked like a dream <3

  13. RD Said on Jan 23, 2010 :

    Thanks everyone! You made it so easy…

  14. omdb Said on Apr 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 Jun 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.

1 Trackback(s)

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

Post a Comment

Write Article for Dev102

Write for Dev102!

We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution

Learn More