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

本文翻译自:Convert seconds to Hour:Minute:Second

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? 我怎样才能做到这一点?


#1楼

参考:https://stackoom.com/question/DJGe/将秒转换为小时-分钟-秒


#2楼

// 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

#3楼

Just in case anyone else is looking for a simple function to return this nicely formatted (I know it is not the format the OP asked for), this is what I've just come up with. 以防万一其他人正在寻找一个简单的函数来返回这个格式很好的(我知道它不是OP要求的格式),这就是我刚刚提出的。 Thanks to @mughal for the code this was based on. 感谢@mughal的代码,这是基于。

function format_timer_result($time_in_seconds){
    $time_in_seconds = ceil($time_in_seconds);

    // Check for 0
    if ($time_in_seconds == 0){
        return 'Less than a second';
    }

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

    // Hours
    $hours = floor($time_in_seconds / (60 * 60));
    $time_in_seconds -= $hours * (60 * 60);

    // Minutes
    $minutes = floor($time_in_seconds / 60);
    $time_in_seconds -= $minutes * 60;

    // Seconds
    $seconds = floor($time_in_seconds);

    // Format for return
    $return = '';
    if ($days > 0){
        $return .= $days . ' day' . ($days == 1 ? '' : 's'). ' ';
    }
    if ($hours > 0){
        $return .= $hours . ' hour' . ($hours == 1 ? '' : 's') . ' ';
    }
    if ($minutes > 0){
        $return .= $minutes . ' minute' . ($minutes == 1 ? '' : 's') . ' ';
    }
    if ($seconds > 0){
        $return .= $seconds . ' second' . ($seconds == 1 ? '' : 's') . ' ';
    }
    $return = trim($return);

    return $return;
}

#4楼

Anyone whose looking for this in the future, this gives the format the initial poster asked for. 任何人在将来寻找这个,这给出了初始海报要求的格式。

$init = 685;
$hours = floor($init / 3600);
$hrlength=strlen($hours);
if ($hrlength==1) {$hrs="0".$hours;}
else {$hrs=$hours;} 

$minutes = floor(($init / 60) % 60);
$minlength=strlen($minutes);
if ($minlength==1) {$mins="0".$minutes;}
else {$mins=$minutes;} 

$seconds = $init % 60;
$seclength=strlen($seconds);
if ($seclength==1) {$secs="0".$seconds;}
else {$secs=$seconds;} 

echo "$hrs:$mins:$secs";

#5楼

Use function gmdate() only if seconds are less than 86400 (1 day) : 仅当秒小于86400 (1天)时才使用函数gmdate()

$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* : 将秒转换为'脚' 无限制格式*

$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 DateTime extension: 使用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:59 to 838:59:59 : 结果的MySQL示例范围限制为TIME数据类型的范围,即-838:59:59838: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 运行演示


#6楼

The gmtdate() function didn't work for me as I was tracking hours worked on a project and if it's over 24 hours, you get amount left over after 24 hours is subtracted. gmtdate()函数对我来说不起作用,因为我正在跟踪项目的工作时间,如果超过24小时,则会减去24小时后剩余的金额。 In other words 37 hours becomes 13 hours. 换句话说,37小时变为13小时。 (all as stated above by Glavic - thanks for your examples!) This one worked well: (如上所述,Glavic - 感谢你的例子!)这个很好用:

Convert seconds to format by 'foot' no limit :
$seconds = 8525;
$H = floor($seconds / 3600);
$i = ($seconds / 60) % 60;
$s = $seconds % 60;
echo sprintf("%02d:%02d:%02d", $H, $i, $s);
# 02:22:05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值