时间到秒 php,时间-输出以秒为单位。 在php中转换为hh:mm:ss格式

时间-输出以秒为单位。 在php中转换为hh:mm:ss格式

我的输出格式为290.52262423327秒。 我如何将其更改为00:04:51?

我想以秒和HH:MM:SS格式显示相同的输出,所以如果是秒,我只想显示290.52秒。(小数点后仅两个整数)? 我怎样才能做到这一点?

我正在使用PHP工作,输出存在于$time变量中。 想要将此$time更改为$newtime,HH:MM:SS和$newsec为290.52。

谢谢 :)

Scorpion King asked 2020-07-28T22:20:24Z

11个解决方案

96 votes

1)

function foo($seconds) {

$t = round($seconds);

return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);

}

echo foo('290.52262423327'), "\n";

echo foo('9290.52262423327'), "\n";

echo foo(86400+120+6), "\n";

版画

00:04:51

02:34:51

24:02:06

2)

echo round($time, 2);

VolkerK answered 2020-07-28T22:20:57Z

14 votes

试试这个

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

Azam Alvi answered 2020-07-28T22:21:17Z

4 votes

$iSeconds = 290.52262423327;

print date('H:i:s', mktime(0, 0, $iSeconds));

Teekin answered 2020-07-28T22:21:33Z

2 votes

试试这个:

$time = 290.52262423327;

echo date("h:i:s", mktime(0,0, round($time) % (24*3600)));

Martin Vseticka answered 2020-07-28T22:21:52Z

1 votes

我不知道这是否是最有效的方法,但是如果您还需要显示日期,则可以这样做:

function foo($seconds) {

$t = round($seconds);

return sprintf('%02d %02d:%02d:%02d', ($t/86400%24), ($t/3600) -(($t/86400%24)*24),($t/60%60), $t%60);

}

Serge answered 2020-07-28T22:22:12Z

1 votes

试试这个 :)

private function conversionTempsEnHms($tempsEnSecondes)

{

$h = floor($tempsEnSecondes / 3600);

$reste_secondes = $tempsEnSecondes - $h * 3600;

$m = floor($reste_secondes / 60);

$reste_secondes = $reste_secondes - $m * 60;

$s = round($reste_secondes, 3);

$s = number_format($s, 3, '.', '');

$h = str_pad($h, 2, '0', STR_PAD_LEFT);

$m = str_pad($m, 2, '0', STR_PAD_LEFT);

$s = str_pad($s, 6, '0', STR_PAD_LEFT);

$temps = $h . ":" . $m . ":" . $s;

return $temps;

}

Zagloo answered 2020-07-28T22:22:32Z

0 votes

Numero uno ... [http://www.ckorp.net/sec2time.php](使用此功能)

第二... echo round(290.52262423327,2);

Dejan Marjanovic answered 2020-07-28T22:22:56Z

0 votes

1)

$newtime = sprintf( "%02d:%02d:%02d", $time / 3600, $time / 60 % 60, $time % 60 );

2)

$newsec = sprintf( "%.2f", $time );

Rob K answered 2020-07-28T22:23:20Z

0 votes

就个人而言,我拒绝回答别人,所以我做了自己的解析器。可以工作几天,几小时,几分钟和几秒钟。 并且应该易于扩展到数周/月等。它也可以反序列化到C#

function secondsToTimeInterval($seconds) {

$t = round($seconds);

$days = floor($t/86400);

$day_sec = $days*86400;

$hours = floor( ($t-$day_sec) / (60 * 60) );

$hour_sec = $hours*3600;

$minutes = floor((($t-$day_sec)-$hour_sec)/60);

$min_sec = $minutes*60;

$sec = (($t-$day_sec)-$hour_sec)-$min_sec;

return sprintf('%02d:%02d:%02d:%02d', $days, $hours, $minutes, $sec);

}

PassiveModding answered 2020-07-28T22:23:40Z

0 votes

如果您正在使用Carbon(例如Laravel中),则可以执行以下操作:

$timeFormatted = date("H:i:s", $seconds);

但是$timeFormatted = date("H:i:s", $seconds);可能已经足够好了。

请注意警告。

Ryan answered 2020-07-28T22:24:09Z

-1 votes

echo date('H:i:s',$time);

echo number_format($time,2);

Mark Baker answered 2020-07-28T22:24:24Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值