如何将秒转换为时间格式?

时间:2010-10-04 14:42:13

标签: php time format

出于某种原因,我转换了一种时间格式,如:03:30 to seconds 3*3600 + 30*60, now。我想把它转换回它的第一个(相同)格式。怎么会这样?

我的尝试:

3*3600 + 30*60 = 12600 

12600 / 60 = 210 / 60 = 3.5, floor(3.5) = 3 = hour

现在,分钟呢?

考虑到值可以像19:00 or 02:51. 我想你已经了解了。

顺便说一下,如何转换2:0 for example to 02:00 using RegEx?

15 个答案:

答案 0 :(得分:153)

这可能更简单

gmdate("H:i:s", $seconds)

PHP gmdate

答案 1 :(得分:115)

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

如果您想获得时间格式:

$timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);

答案 2 :(得分:14)

如果您知道时间不到一小时,则可以使用date()$date->format()功能。

$minsandsecs = date('i:s',$numberofsecs);

这是因为系统纪元时间从午夜开始(1970年1月1日,但这对你来说并不重要)。

如果是一个小时或更长但不到一天,您可以在几小时内输出:mins:secs格式为`

$hoursminsandsecs = date('H:i:s',$numberofsecs);

超过一天,您需要使用模数来计算天数,因为这是该时期的开始日期将变得相关的地方。

希望有所帮助。

答案 3 :(得分:11)

也许最简单的方法是:

gmdate('H:i:s', $your_time_in_seconds);

答案 4 :(得分:10)

$time为秒数。

$seconds = $time % 60;
$time = ($time - $seconds) / 60;
$minutes = $time % 60;
$hours = ($time - $minutes) / 60;

现在,小时,分钟和秒分别位于$hours$minutes$seconds

答案 5 :(得分:8)

另一种解决方案,可以为您提供传入秒数值的天数,小时数,分钟数和秒数:

function seconds_to_time($secs)
{
    $dt = new DateTime('@' . $secs, new DateTimeZone('UTC'));
    return array('days'    => $dt->format('z'),
                 'hours'   => $dt->format('G'),
                 'minutes' => $dt->format('i'),
                 'seconds' => $dt->format('s'));
}

print_r(seconds_to_time($seconds_value);

如果预计时间超过一年,“天”将需要额外的逻辑。使用str_pad()ltrim()添加/删除前导零。

答案 6 :(得分:7)

当您想使用此代码将秒数转换为小时:分钟:秒

等时间格式时,ITroubs的答案不会处理剩下的秒数

以下是我采取的措施: (这也会将前导零加到一位数分钟和秒)

$seconds = 3921; //example

$hours = floor($seconds / 3600);
$mins = floor(($seconds - $hours*3600) / 60);
$s = $seconds - ($hours*3600 + $mins*60);

$mins = ($mins<10?"0".$mins:"".$mins);
$s = ($s<10?"0".$s:"".$s); 

$time = ($hours>0?$hours.":":"").$mins.":".$s;

$ time将在此示例中包含“1:05:21”。

答案 7 :(得分:6)

如果您要硬编码,您可以使用模数来提取其他人建议的时间。

如果你从MySQL数据库返回秒数,假设你的应用程序中不需要秒格式的数据,那么有一个更简洁的方法,你可以使用MySQL的SEC_TO_TIME它将会以hh:mm:ss格式返回时间。

例如

SELECT SEC_TO_TIME(my_seconds_field) AS my_timestring;

答案 8 :(得分:3)

这样的事情?

 if(is_numeric($time)){
$value = array(
  "years" => 0, "days" => 0, "hours" => 0,
  "minutes" => 0, "seconds" => 0,
);
if($time >= 31556926){
  $value["years"] = floor($time/31556926);
  $time = ($time%31556926);
}
if($time >= 86400){
  $value["days"] = floor($time/86400);
  $time = ($time%86400);
}
if($time >= 3600){
  $value["hours"] = floor($time/3600);
  $time = ($time%3600);
}
if($time >= 60){
  $value["minutes"] = floor($time/60);
  $time = ($time%60);
}
$value["seconds"] = floor($time);
return (array) $value;

}否则{     return(bool)FALSE;   }

抓住:http://www.ckorp.net/sec2time.php

答案 9 :(得分:3)

对不起,这太晚了,但也许有用

function mediaTimeDeFormater($seconds)
{
    if (!is_numeric($seconds))
        throw new Exception("Invalid Parameter Type!");


    $ret = "";

    $hours = (string )floor($seconds / 3600);
    $secs = (string )$seconds % 60;
    $mins = (string )floor(($seconds - ($hours * 3600)) / 60);

    if (strlen($hours) == 1)
        $hours = "0" . $hours;
    if (strlen($secs) == 1)
        $secs = "0" . $secs;
    if (strlen($mins) == 1)
        $mins = "0" . $mins;

    if ($hours == 0)
        $ret = "$mins:$secs";
    else
        $ret = "$hours:$mins:$secs";

    return $ret;
}

echo mediaTimeDeFormater(216.064000);//3:36

答案 10 :(得分:2)

使用modulo:

$hours = $time_in_seconds / 3600;
$minutes = ($time_in_seconds / 60) % 60;

答案 11 :(得分:2)

只需一个小的额外示例请求的时间(以毫秒为单位)

    // ms2time( (microtime(true) - ( time() - rand(0,1000000) ) ) );
    // return array
    function ms2time($ms){
        $return = array();
        // ms
        $return['ms'] = (int) number_format( ($ms - (int) $ms), 2, '', '');
        $seconds = (int) $ms;
        unset($ms);

        if ($seconds%60 > 0){
            $return['s'] = $seconds%60;
        } else {
            $return['s'] = 0;
        }

        if ( ($minutes = intval($seconds/60))){
            $return['m'] = $minutes;
        }

        if (isset($return['m'])){
            $return['h'] = intval($return['m'] / 60);
            $return['m']  = $return['m'] % 60; 
        }


        if (isset($return['h'])){
            $return['d'] = intval($return['h'] / 24);
            $return['h']  = $return['h'] % 24; 
        }

        if (isset($return['d']))
            $return['mo'] = intval($return['d'] / 30);

        foreach($return as $k=>$v){
            if ($v == 0)
                unset($return[$k]);
        }

        return $return;
    }

    // ms2time2string( (microtime(true) - ( time() - rand(0,1000000) ) ) );
    // return array     
    function ms2time2string($ms){
        $array = array(
            'ms' => 'ms',
            's'  => 'seconds',
            'm'  => 'minutes',
            'h'  => 'hours',
            'd'  => 'days',
            'mo' => 'month',
        );


        if ( ( $return = ms2time($ms) )  && count($ms) > 0){

            foreach($return as $key=>$data){
                $return[$key] = $data .' '.$array[$key];
            }

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

答案 12 :(得分:2)

这是另一种方法,对所有人都是'0'。

$secCount = 10000;
$hours = str_pad(floor($secCount / (60*60)), 2, '0', STR_PAD_LEFT);
$minutes = str_pad(floor(($secCount - $hours*60*60)/60), 2, '0', STR_PAD_LEFT);
$seconds = str_pad(floor($secCount - ($hours*60*60 + $minutes*60)), 2, '0', STR_PAD_LEFT);

这是对Flaxious答案的改编。

答案 13 :(得分:-2)

如果你想要很好的格式,如:0:00:00使用str_pad()作为@Gardner。

答案 14 :(得分:-6)

1天= 86400000毫秒。

DecodeTime(毫秒/ 86400000,小时,分钟,秒,毫秒)

盛衰!我在delphi中思考,所有语言都必须有类似的东西。

相关问题