0

I am trying to convert a number into hour,min.But Its not working well. I have variable which has value something like

$t_hr = 193;

I want to display it like 3 hr 13 min I tried this code

$t_min = 193;
if($t_min>60)
   {
    $t_hr=$t_hr+1;
    $t_min = $t_min-60;
   }

which is not working well.I need to get the value like above.Any Idea?

Wolverine
  • 71
  • 3
  • 11

3 Answers3

6

Simply try like this

echo floor($t_hr/60)." hr ".($t_hr%60)." min";

Assuming that $t_hr is the minutes

GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

First, you have to determine the number of hours. This can be easily done with a division:

$t_hr = floor($t_min / 60); // floor(193 / 60) = floor(3.2166...) = 3

Then, you can use modulo (which returns the remainder of the division) to get the number of minutes:

$t_min = $t_min % 60; // 193 % 60 = 13
Sebastien C.
  • 4,649
  • 1
  • 21
  • 32
0

Here's my time_format() function, based on someone else's work from years ago

function time_format($seconds, $mode = "long", $extra = ''){
    $names    = array('long' => array("year", "month", "day", "hour", "minute", "second"), 'short' => array("yr", "mnth", "day", "hr", "min", "sec"));

    $seconds  = floor($seconds);

    $minutes  = intval($seconds / 60);
    $seconds -= ($minutes * 60);

    $hours    = intval($minutes / 60);
    $minutes -= ($hours * 60);

    $days     = intval($hours / 24);
    $hours   -= ($days * 24);

    $months   = intval($days / 31);
    $days    -= ($months * 31);

    $years    = intval($months / 12);
    $months  -= ($years * 12);

    $result   = array();
    if ($years)
        $result[] = sprintf("%s%s %s%s", number_format($years), ' '.$extra, $names[$mode][0], $years == 1 ? "" : "s");
    if ($months)
        $result[] = sprintf("%s%s %s%s", number_format($months), ' '.$extra, $names[$mode][1], $months == 1 ? "" : "s");
    if ($days)
        $result[] = sprintf("%s%s %s%s", number_format($days), ' '.$extra, $names[$mode][2], $days == 1 ? "" : "s");
    if ($hours)
        $result[] = sprintf("%s%s %s%s", number_format($hours), ' '.$extra, $names[$mode][3], $hours == 1 ? "" : "s");
    if ($minutes && count($result) < 2)
        $result[] = sprintf("%s%s %s%s", number_format($minutes), ' '.$extra, $names[$mode][4], $minutes == 1 ? "" : "s");
    if (($seconds && count($result) < 2) || !count($result))
        $result[] = sprintf("%s%s %s%s", number_format($seconds), ' '.$extra, $names[$mode][5], $seconds == 1 ? "" : "s");

    return implode(", ", $result);
}

echo time_format(193),'<br />';
echo time_format(193, 'short'),'<br />';
echo time_format(193, 'long', 'special'),'<br />';

Should return:
3 hours, 13 minutes
3 hrs, 13 mins
3 special hours, 13 minutes

Magictallguy
  • 622
  • 4
  • 16