将秒转换为更具体格式的便捷方式

时间:2011-06-02 05:22:27

标签: php time

有没有方便的方法将秒转换为更具体的格式。

例如:

我想转换

559 seconds

9 min 19 sec

6 个答案:

答案 0 :(得分:2)

$seconds = 559;

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

答案 1 :(得分:2)

gmdate可以使用

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

答案 2 :(得分:1)

请参阅http://dev.kafol.net/2008/09/php-calculating-time-span-duration.html

 $days = floor($seconds/60/60/24);
 $hours = $seconds/60/60%24;
 $mins = $seconds/60%60;
 $secs = $seconds%60;

 $duration='';
 if($days>0) $duration .= "$days days ";
 if($hours>0) $duration .= "$hours hours ";
 if($mins>0) $duration .= "$mins minutes ";
 if($secs>0) $duration .= "$secs seconds ";

答案 3 :(得分:1)

以下是我使用AMX Mod X附带的一些SmallC本机代码制作的PHP版本。

<?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]);
        }
    }
}

?>

答案 4 :(得分:1)

使用gmdate("H:i:s",$foo)的唯一缺点是它只适用于0到86399之间的$foo,并且它会为您提供前导零,您可能想要也可能不想要它们。这是我使用的解决方案,当我们的时间跨度很大时,它只使用gmdate()

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;
}

用法/输出如下:

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

显然,如果您希望输出更像1 hr, 1 min, 7 sec,则可以完全取出gmdate()并修改格式/字符串以满足您的品味/需求。

小警告,但是:这不检查“单位”应该是单数还是复数,因此我使用了“sec”,“min”,等缩写。此外,它不检查任何单位是否有0;您可能希望将1 min, 0 sec简单地表达为1 min

答案 5 :(得分:0)

你可以这样做:

$sec = 559;

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