我的输出格式为290.52262423327秒。我如何将其更改为00:04:51?
我希望以秒和HH:MM:SS格式显示相同的输出,所以如果是秒,我想只显示290.52秒(小数点后只有两个整数)?我该怎么做?
我在php中工作,输出存在于$time
变量中。想要将此$time
更改为$newtime
,其中HH:MM:SS和$newsec
为290.52。
谢谢:)
答案 0 :(得分:89)
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);
答案 1 :(得分:12)
试试这个
echo gmdate("H:i:s", 90);
答案 2 :(得分:4)
$iSeconds = 290.52262423327;
print date('H:i:s', mktime(0, 0, $iSeconds));
答案 3 :(得分:2)
试试这个:
$time = 290.52262423327;
echo date("h:i:s", mktime(0,0, round($time) % (24*3600)));
答案 4 :(得分:2)
在23:59:59
小时内,您可以使用PHP默认功能
echo gmdate("H:i:s", 86399);
只会返回结果,直到23:59:59
如果您的秒数大于86399, @VolkerK答案的帮助
$time = round($seconds);
echo sprintf('%02d:%02d:%02d', ($time/3600),($time/60%60), $time%60);
将是使用的最佳选择...
答案 5 :(得分:1)
我不知道这是否是最有效的方式,但是如果你还需要显示日期,这可行:
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);
}
答案 6 :(得分:1)
试试这个:)
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;
}
答案 7 :(得分:0)
Numero uno ... http://www.ckorp.net/sec2time.php(使用此功能)
Numero duo ... echo round(290.52262423327,2);
答案 8 :(得分:0)
1)
$newtime = sprintf( "%02d:%02d:%02d", $time / 3600, $time / 60 % 60, $time % 60 );
2)
$newsec = sprintf( "%.2f", $time );
答案 9 :(得分:0)
就个人而言,我回答了我自己编写的解析器。 可以工作几天,几小时,几分钟和几秒钟。并且应该易于扩展到数周/月等。 它也可以反序列化为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);
}
答案 10 :(得分:0)
如果您使用Carbon(例如Laravel中的碳),则可以执行以下操作:
$timeFormatted = \Carbon\Carbon::now()->startOfDay()->addSeconds($seconds)->toTimeString();
但是$timeFormatted = date("H:i:s", $seconds);
可能已经足够了。
仅见caveats。
答案 11 :(得分:0)
基于https://stackoverflow.com/a/3534705/4342230,但增加了天数:
function durationToString($seconds) {
$time = round($seconds);
return sprintf(
'%02dD:%02dH:%02dM:%02dS',
$time / 86400,
($time / 3600) % 24,
($time / 60) % 60,
$time % 60
);
}
答案 12 :(得分:0)
这是我的微秒实现
/**
* @example 00 d 00 h 00 min 00 sec 005098 ms (0.005098 sec.ms)
*/
public function __toString()
{
// Add your code to get $seconds and $microseconds
$time = round(($seconds + $microseconds), 6, PHP_ROUND_HALF_UP);
return sprintf(
'%02d d %02d h %02d min %02d sec %06d ms (%s sec.ms)',
$time / 86400,
($time / 3600) % 24,
($time / 60) % 60,
$time % 60,
$time * 1000000 % 1000000,
$time
);
}
答案 13 :(得分:-1)
echo date('H:i:s',$time);
echo number_format($time,2);