2011-06-02 46 views

回答

2

gmdate可用於

echo gmdate("H:i:s", 559); 
+2

+1,最好的代碼是你不必寫的代碼。 – 2011-06-02 05:27:25

+1

秒數爲86,401時會發生什麼? -1。 – ceejayoz 2011-06-02 18:14:55

2
$seconds = 559; 

$minutes = floor($seconds/60); // 9 
$seconds = $seconds % 60;   // 19 
+1

我想你的意思是'$分鐘= floor($秒/ 60);'因爲它不是Python? – Xaerxess 2011-06-02 05:48:55

+0

啊,我想我可以,謝謝 – 2011-06-02 18:13:49

1

下面是一個PHP版本,我做了隨機附帶AMX Mod X一些的SmallC本地代碼。

<?php 
/** 
* PHPInSimMod - Time Module 
* @package PRISM 
* @subpackage Time 
* @copyright the AMX Mod X Development Team & The PRISM Devlopment Team. 
*/ 

class Time 
{ 
    // Time unit types. 
    const UNIT_SECONDS  = 0; 
    const UNIT_MINUTES  = 1; 
    const UNIT_HOURS  = 2; 
    const UNIT_DAYS   = 3; 
    const UNIT_WEEKS  = 4; 

    // The number of seconds that are in each time unit. 
    const SECONDS_IN_MINUTE = 60; 
    const SECONDS_IN_HOUR = 3600; 
    const SECONDS_IN_DAY = 86400; 
    const SECONDS_IN_WEEK = 604800; 

    /** 
    * @desc: By Brad for AMX Mod X. 
    * @param: Unit - The number of time units you want translated into verbose text. 
    * @param: Type - The type of unit (i.e. seconds, minutes, hours, days, weeks) that you are passing in. 
    */ 
    static function getLength($unit, $type = Time::UNIT_SECONDS) 
    { 
     # Ensure the varables are of the correct datatype. 
     $unit = (int) $unit; 
     $type = (int) $type; 

     # If our units happens to equal zero, skip. 
     if ($unit == 0) 
      return '0 seconds'; 

     # Determine the number of each time unit there are. 
     $weeks = 0; $days = 0; $hours = 0; $minutes = 0; $seconds = 0; 

     # Handle the various scales of time. 
     switch ($type) 
     { 
      case Time::UNIT_SECONDS: $seconds = $unit; 
      case Time::UNIT_MINUTES: $seconds = $unit * Time::SECONDS_IN_MINUTE; 
      case Time::UNIT_HOURS: $seconds = $unit * Time::SECONDS_IN_HOUR; 
      case Time::UNIT_DAYS: $seconds = $unit * Time::SECONDS_IN_DAY; 
      case Time::UNIT_WEEKS: $seconds = $unit * Time::SECONDS_IN_WEEK; 
     } 

     # How many weeks left? 
     $weeks = $seconds/Time::SECONDS_IN_WEEK; 
     $seconds -= ($weeks * Time::SECONDS_IN_WEEK); 

     # How many days left? 
     $days = $seconds/Time::SECONDS_IN_DAY; 
     $seconds -= ($days * Time::SECONDS_IN_DAY); 

     # How many hours left? 
     $hours = $seconds/Time::SECONDS_IN_HOUR; 
     $seconds -= ($hours * Time::SECONDS_IN_HOUR); 

     # How many minutes left? 
     $minutes = $seconds/Time::SECONDS_IN_MINUTE; 
     $seconds -= ($minutes * Time::SECONDS_IN_MINUTE); 

     # Seconds are the base unit, so it's handled intrinsically 

     // Translate the unit counts into verbose text 
     $timeElement = array(); 

     if ($weeks > 0) 
      $timeElement[] = sprintf("%i %s", $weeks, ($weeks == 1) ? "week" : "weeks"); 
     if ($days > 0) 
      $timeElement[] = sprintf("%i %s", $days, ($days == 1) ? "day" : "days"); 
     if ($hours > 0) 
      $timeElement[] = sprintf("%i %s", $hours, ($hours == 1) ? "hour" : "hours"); 
     if ($minutes > 0) 
      $timeElement[] = sprintf("%i %s", $minutes, ($minutes == 1) ? "minute" : "minutes"); 
     if ($seconds > 0) 
      $timeElement[] = sprintf("%i %s", $seconds, ($seconds == 1) ? "second" : "seconds"); 

     // Outputs are final result in the correct format. 
     switch(count($timeElement)) 
     { 
      case 1: return sprintf("%s", $timeElement[0]); 
      case 2: return sprintf("%s & %s", $timeElement[0], $timeElement[1]); 
      case 3: return sprintf("%s, %s & %s", $timeElement[0], $timeElement[1], $timeElement[2]); 
      case 4: return sprintf("%s, %s, %s & %s", $timeElement[0], $timeElement[1], $timeElement[2], $timeElement[3]); 
      case 5: return sprintf("%s, %s, %s, %s & %s", $timeElement[0], $timeElement[1], $timeElement[2], $timeElement[3], $timeElement[4]); 
     } 
    } 
} 

?> 
0

你可以這樣做:

$sec = 559; 

$min = floor($sec/60); 
$sec = $sec % 60; // 19 
+0

我對格式化表示歉意,目前我在iPhone上。 – 2011-06-02 05:34:22

1

唯一的缺點使用gmdate("H:i:s",$foo)的是,它僅適用於0和86399之間$foo和它給你前導零,你可能會或可能不希望他們。下面是我使用的解決方案,它僅使用gmdate()當我們的時間跨度很大:如果你想輸出更喜歡1 hr, 1 min, 7 sec,你可以

echo formatTimeSpan(3);  // 3 sec 
echo formatTimeSpan(59);  // 59 sec 
echo formatTimeSpan(60);  // 1 min, 0 sec 
echo formatTimeSpan(559); // 9 min, 19 sec 
echo formatTimeSpan(3667); // 1:01:07 
echo formatTimeSpan(85483); // 23:44:43 
echo formatTimeSpan(86901); // 1 d, 00:08:21 

顯然,:

function formatTimeSpan($span) { 
    if ($span < (60)) { //--- less than a minute 
    $r = $span." sec"; # just use the seconds as they are 
    } 
    elseif ($span < (60*60)) { //--- a minute to under an hour 
    $min = intval($span/60); 
    $sec = $span - ($min*60); 
    $r = $min." min, ".$sec." sec"; 
    } 
    elseif ($span < (24*60*60)) { //--- an hour to under a day 
    $r = gmdate("G:i:s",$span); 
    } 
    else { //--- a day or more 
    $days = intval($span/(24*60*60)); 
    $remSec = $span - ($days * (24*60*60)); 
    $r = $days." d, ".gmdate("G:i:s",$remSec); 
    } 

    return $r; 
} 

用法/輸出如下完全取出gmdate()並修改格式/字符串以適合您的口味/需求。

小告誡,但:這並不檢查以查看是否「單位」應該是單數或複數,因此,我使用的縮寫如「秒」,「分鐘」,另外,它不不檢查任何單位是否有0;你可能更願意將1 min, 0 sec簡單地表示爲1 min

相關問題