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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3856293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:19:27  来源:igfitidea点击:

How to convert seconds to time format?

phptimeformat

提问by Dewan159

For some reason I convert a time format like: 03:30 to seconds 3*3600 + 30*60, now. I wanna convert it back to its first (same) format up there. How could that be?

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

My attempt:

我的尝试:

3*3600 + 30*60 = 12600 

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

Now, what about the minutes?

现在,分钟呢?

Considering the value can be like 19:00 or 02:51.I think you got the picture.

考虑到价值可能就像19:00 or 02:51.我认为你得到了图片。

And by the way, how to convert 2:0 for example to 02:00 using RegEx?

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

回答by CyberJunkie

This might be simpler

这可能更简单

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

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

PHP gmdate

PHP 更新日期

回答by ITroubs

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

If you want to get time format:

如果要获取时间格式:

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

回答by Spudley

If the you know the times will be less than an hour, you could just use the date()or $date->format()functions.

如果您知道时间将少于一个小时,您可以使用date()or$date->format()函数。

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

This works because the system epoch time begins at midnight (on 1 Jan 1970, but that's not important for you).

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

If it's an hour or more but less than a day, you could output it in hours:mins:secs format with `

如果它是一个小时或更长时间但不到一天,您可以使用 `hours:mins:secs 格式输出它

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

For more than a day, you'll need to use modulus to calculate the number of days, as this is where the start date of the epoch would become relevant.

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

Hope that helps.

希望有帮助。

回答by marco_manti

Maybe the simplest way is:

也许最简单的方法是:

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

回答by Hammerite

Let $timebe the time as number of seconds.

$time时间作为秒数。

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

Now the hours, minutes and seconds are in $hours, $minutesand $secondsrespectively.

现在的小时,分钟和秒都在$hours$minutes$seconds分别。

回答by GZipp

Another solution that will give you the days, hours, minutes, and seconds for a passed-in seconds value:

另一个解决方案将为您提供传入秒值的天数、小时数、分钟数和秒数:

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

Extra logic will be needed for 'days' if the time is expected to be more than one year. Use str_pad()or ltrim()to add/remove leading zeros.

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

回答by Flaxious

ITroubs answer doesn't deal with the left over seconds when you want to use this code to convert an amount of seconds to a time format like hours : minutes : seconds

当您想使用此代码将秒数转换为时间格式时,ITroubs 的答案不会处理剩余的秒数: hours : minutes : seconds

Here is what I did to deal with this: (This also adds a leading zero to one-digit minutes and seconds)

这是我为解决这个问题所做的:(这还将前导零添加到一位数的分钟和秒)

$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 will contain "1:05:21" in this example.

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

回答by Julijan An?eli?

If you were to hardcode it you would use modulus to extract the time as others suggested.

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

If you are returning the seconds from MySQL database, assuming you don't need the data in seconds format in your app, there is a much cleaner way to do it, you can use MySQL's SEC_TO_TIMEand it will return time in hh:mm:ss format.

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

Eg.

例如。

SELECT SEC_TO_TIME(my_seconds_field) AS my_timestring;

回答by FatherStorm

something like this?

像这样的东西?

 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;

}else{ return (bool) FALSE; }

}else{ 返回(布尔)假;}

grabbed from: http://www.ckorp.net/sec2time.php

抓取自:http: //www.ckorp.net/sec2time.php

回答by Soroush Khosravi

Sorry this is too late but maybe useful

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

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