2014-02-27 85 views
0

我試圖將一個數字轉換爲小時,分鐘,但它不能很好地工作。 我有具有價值的東西像如何使用PHP將數字轉換爲小時和分鐘?

$t_hr = 193; 

我要像3 hr 13 min 顯示它我想這個代碼

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

這是不工作well.I需要得到像上述變量的值。任何想法?

回答

4

簡單地嘗試這樣

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

假設$t_hrminutes

0

首先,你要確定的小時數。這可以用一個師很容易做到:

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

然後,您可以使用取模(返回除法的餘數)來獲取分鐘數:

$t_min = $t_min % 60; // 193 % 60 = 13 
0

這裏是我的TIME_FORMAT()功能的基礎上,別人的工作從年前

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 />'; 

應返回:
3個小時,13分鐘
3小時13分鐘
3小時13分鐘

相關問題