2011-06-02 45 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