php 将秒转换为小时:分钟:秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3172332/
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 08:54:55  来源:igfitidea点击:

Convert seconds to Hour:Minute:Second

phptime

提问by M.E

I need to convert seconds to "Hour:Minute:Second".

我需要将秒转换为“小时:分钟:秒”。

For example: "685" converted to "00:11:25"

例如:“685”转换为“00:11:25”

How can I achieve this?

我怎样才能做到这一点?

回答by animuson

You can use the gmdate()function:

您可以使用该gmdate()功能:

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

回答by Aif

One hour is 3600sec, one minute is 60sec so why not:

一小时是 3600 秒,一分钟是 60 秒,为什么不呢:

<?php

$init = 685;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;

echo "$hours:$minutes:$seconds";

?>

which produces:

它产生:

$ php file.php
0:11:25

(I've not tested this much, so there might be errors with floor or so)

(我没有测试过这么多,所以地板上可能会有错误)

回答by nathan

here you go

干得好

function format_time($t,$f=':') // t = seconds, f = separator 
{
  return sprintf("%02d%s%02d%s%02d", floor($t/3600), $f, ($t/60)%60, $f, $t%60);
}

echo format_time(685); // 00:11:25

回答by Glavi?

Use function gmdate()only if seconds are less than 86400(1 day):

gmdate()仅当秒数小于86400(1 天)时才使用函数:

$seconds = 8525;
echo gmdate('H:i:s', $seconds);
# 02:22:05

See: gmdate()

请参阅:gmdate()

Run the Demo

运行演示



Convert seconds to format by 'foot' no limit*:

将秒数转换为 'foot' 格式无限制*

$seconds = 8525;
$H = floor($seconds / 3600);
$i = ($seconds / 60) % 60;
$s = $seconds % 60;
echo sprintf("%02d:%02d:%02d", $H, $i, $s);
# 02:22:05

See: floor(), sprintf(), arithmetic operators

请参阅:floor()sprintf()算术运算符

Run the Demo

运行演示



Example use of DateTimeextension:

DateTime扩展名的使用示例:

$seconds = 8525;
$zero    = new DateTime("@0");
$offset  = new DateTime("@$seconds");
$diff    = $zero->diff($offset);
echo sprintf("%02d:%02d:%02d", $diff->days * 24 + $diff->h, $diff->i, $diff->s);
# 02:22:05

See: DateTime::__construct(), DateTime::modify(), clone, sprintf()

请参阅:DateTime::__construct()DateTime::modify()clonesprintf()

Run the Demo

运行演示



MySQL example range of the result is constrained to that of the TIME data type, which is from -838:59:59to 838:59:59:

结果的 MySQL 示例范围被限制为 TIME 数据类型的范围,即 from -838:59:59to838:59:59

SELECT SEC_TO_TIME(8525);
# 02:22:05

See: SEC_TO_TIME

请参阅:SEC_TO_TIME

Run the Demo

运行演示



PostgreSQL example:

PostgreSQL 示例:

SELECT TO_CHAR('8525 second'::interval, 'HH24:MI:SS');
# 02:22:05

Run the Demo

运行演示

回答by RaDim

Other solutions use gmdate, but fail in edge cases where you have more than 86400 seconds. To get around this, we can simply compute the number of hours ourselves, then let gmdatecompute the remaining seconds into minutes/seconds.

其他解决方案使用gmdate,但在超过 86400 秒的边缘情况下失败。为了解决这个问题,我们可以简单地自己gmdate计算小时数,然后将剩余的秒数计算为分钟/秒。

echo floor($seconds / 3600) . gmdate(":i:s", $seconds % 3600);

Input: 6030Output: 1:40:30

输入:6030输出:1:40:30

Input: 2000006030Output: 555557:13:50

输入:2000006030输出:555557:13:50

回答by mughal

// TEST
// 1 Day 6 Hours 50 Minutes 31 Seconds ~ 111031 seconds

$time = 111031; // time duration in seconds

$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);

$hours = floor($time / (60 * 60));
$time -= $hours * (60 * 60);

$minutes = floor($time / 60);
$time -= $minutes * 60;

$seconds = floor($time);
$time -= $seconds;

echo "{$days}d {$hours}h {$minutes}m {$seconds}s"; // 1d 6h 50m 31s

回答by Vikash Kumar

gmdate("H:i:s", no_of_seconds);

Will not give time in H:i:sformat if no_of_secondsis greater than 1 day (seconds in a day).
It will neglect day value and give only Hour:Min:Seconds

H:i:s如果no_of_seconds大于 1 天(一天中的秒数),则不会以格式给出时间。
它会忽略天值而只给出Hour:Min:Seconds

For example:

例如:

gmdate("H:i:s", 89922); // returns 0:58:42 not (1 Day 0:58:42) or 24:58:42

回答by Benjamin

Here is a one liner that handles negative seconds and more than 1 day worth of seconds.

这是一个处理负秒数和超过 1 天的秒数的单线。

sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));

For Example:

例如:

$seconds= -24*60*60 - 2*60*60 - 3*60 - 4; // minus 1 day 2 hours 3 minutes 4 seconds
echo sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));

outputs: -26:03:04

输出:-26:03:04

回答by vamsikrishnamannem

write function like this to return an array

写这样的函数来返回一个数组

function secondsToTime($seconds) {

  // extract hours
  $hours = floor($seconds / (60 * 60));

  // extract minutes
  $divisor_for_minutes = $seconds % (60 * 60);
  $minutes = floor($divisor_for_minutes / 60);

  // extract the remaining seconds
  $divisor_for_seconds = $divisor_for_minutes % 60;
  $seconds = ceil($divisor_for_seconds);

  // return the final array
  $obj = array(
      "h" => (int) $hours,
      "m" => (int) $minutes,
      "s" => (int) $seconds,
   );

  return $obj;
}

then simply call the function like this:

然后简单地像这样调用函数:

secondsToTime(100);

output is

输出是

Array ( [h] => 0 [m] => 1 [s] => 40 )

回答by Eranda

If you don't like accepted answer or popular ones, then try this one

如果你不喜欢接受的答案或受欢迎的答案,那么试试这个

function secondsToTime($seconds_time)
{
    if ($seconds_time < 24 * 60 * 60) {
        return gmdate('H:i:s', $seconds_time);
    } else {
        $hours = floor($seconds_time / 3600);
        $minutes = floor(($seconds_time - $hours * 3600) / 60);
        $seconds = floor($seconds_time - ($hours * 3600) - ($minutes * 60));
        return "$hours:$minutes:$seconds";
    }
}

secondsToTime(108620); // 30:10:20