我需要将秒转换为“小时:分钟:秒”。
例如:“685”转换为“00:11:25”
我怎样才能做到这一点呢?
我需要将秒转换为“小时:分钟:秒”。
例如:“685”转换为“00:11:25”
我怎样才能做到这一点呢?
当前回答
在java中你可以使用这种方法。
private String getHmaa(long seconds) {
String string;
int hours = (int) seconds / 3600;
int remainder = (int) seconds - hours * 3600;
int mins = remainder / 60;
//remainder = remainder - mins * 60;
//int secs = remainder;
if (hours < 12 && hours > 0) {
if (mins < 10) {
string = String.valueOf((hours < 10 ? "0" + hours : hours) + ":" + (mins > 0 ? "0" + mins : "0") + " AM");
} else {
string = String.valueOf((hours < 10 ? "0" + hours : hours) + ":" + (mins > 0 ? mins : "0") + " AM");
}
} else if (hours >= 12) {
if (mins < 10) {
string = String.valueOf(((hours - 12) < 10 ? "0" + (hours - 12) : ((hours - 12) == 12 ? "0" : (hours - 12))) + ":" + (mins > 0 ? "0" + mins : "0") + ((hours - 12) == 12 ? " AM" : " PM"));
} else {
string = String.valueOf(((hours - 12) < 10 ? "0" + (hours - 12) : ((hours - 12) == 12 ? "0" : (hours - 12))) + ":" + (mins > 0 ? mins : "0") + ((hours - 12) == 12 ? " AM" : " PM"));
}
} else {
if (mins < 10) {
string = String.valueOf("0" + ":" + (mins > 0 ? "0" + mins : "0") + " AM");
} else {
string = String.valueOf("0" + ":" + (mins > 0 ? mins : "0") + " AM");
}
}
return string;
}
其他回答
不知道为什么还没有提出这个建议,但是这里有一个使用DateInterval的变体,它的优点是秒> 86400不是问题(只是不要期望小时< 24)
noteable:该函数返回DateInterval对象,因此输出格式由调用者决定
function seconds2interval($seconds) {
$s = intval($seconds);
$hrs = floor($s / 3600);
$min = floor(($s - ($hrs * 3600)) / 60);
$sec = $s % 60;
$duration = sprintf('PT%dH%dM%dS', $hrs, $min, $sec);
$d = new DateInterval($duration);
return $d;
}
$d = seconds2interval(93837.3113);
echo $d->format('%H:%I:%s'); // 26:03:57
解决方案来自:https://gist.github.com/SteveJobzniak/c91a8e2426bac5cb9b0cbc1bdbc45e4b
这段代码尽可能地避免了乏味的函数调用和逐条的字符串构建,以及人们为此而创建的大而笨重的函数。
它返回格式为“1h05m00s”的输出,并使用前导零表示分和秒,只要在它们之前有另一个非零时间组件。
它还跳过所有空的前导组件,以避免给你无用的信息,如“0h00m01s”(相反,它将显示为“1s”)。
示例结果:“1s”、“1m00s”、“19m08s”、“1h00m00s”、“4h08m39s”。
$duration = 1; // values 0 and higher are supported!
$converted = [
'hours' => floor( $duration / 3600 ),
'minutes' => floor( ( $duration / 60 ) % 60 ),
'seconds' => ( $duration % 60 )
];
$result = ltrim( sprintf( '%02dh%02dm%02ds', $converted['hours'], $converted['minutes'], $converted['seconds'] ), '0hm' );
if( $result == 's' ) { $result = '0s'; }
如果你想让代码更短(但可读性更差),你可以避免$转换数组,而是直接将值放在sprintf()调用中,如下所示:
$duration = 1; // values 0 and higher are supported!
$result = ltrim( sprintf( '%02dh%02dm%02ds', floor( $duration / 3600 ), floor( ( $duration / 60 ) % 60 ), ( $duration % 60 ) ), '0hm' );
if( $result == 's' ) { $result = '0s'; }
在上面的两个代码段中,持续时间必须为0或更高。不支持负持续时间。但是你可以使用下面的代码来处理负持续时间:
$duration = -493; // negative values are supported!
$wasNegative = FALSE;
if( $duration < 0 ) { $wasNegative = TRUE; $duration = abs( $duration ); }
$converted = [
'hours' => floor( $duration / 3600 ),
'minutes' => floor( ( $duration / 60 ) % 60 ),
'seconds' => ( $duration % 60 )
];
$result = ltrim( sprintf( '%02dh%02dm%02ds', $converted['hours'], $converted['minutes'], $converted['seconds'] ), '0hm' );
if( $result == 's' ) { $result = '0s'; }
if( $wasNegative ) { $result = "-{$result}"; }
// $result is now "-8m13s"
gmtdate()函数没有为我工作,因为我在跟踪一个项目上的工作时间,如果超过24小时,你会得到24小时后减去的剩余量。换句话说,37小时变成了13小时。(如上所述,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
$given = 685;
/*
* In case $given == 86400, gmdate( "H" ) will convert it into '00' i.e. midnight.
* We would need to take this into consideration, and so we will first
* check the ratio of the seconds i.e. $given:$number_of_sec_in_a_day
* and then after multiplying it by the number of hours in a day (24), we
* will just use "floor" to get the number of hours as the rest would
* be the minutes and seconds anyways.
*
* We can also have minutes and seconds combined in one variable,
* e.g. $min_sec = gmdate( "i:s", $given );
* But for versatility sake, I have taken them separately.
*/
$hours = ( $given > 86399 ) ? '0'.floor( ( $given / 86400 ) * 24 )-gmdate( "H", $given ) : gmdate("H", $given );
$min = gmdate( "i", $given );
$sec = gmdate( "s", $given );
echo $formatted_string = $hours.':'.$min.':'.$sec;
将其转换为函数:
function getHoursFormat( $given ){
$hours = ( $given > 86399 ) ? '0'.floor( ( $given / 86400 ) * 24 )-gmdate( "H", $given ) : gmdate("H", $given );
$min = gmdate( "i", $given );
$sec = gmdate( "s", $given );
$formatted_string = $hours.':'.$min.':'.$sec;
return $formatted_string;
}
如果你需要在javascript中做到这一点,你可以在这里回答的一行代码中做到这一点,用javascript将秒转换为HH-MM-SS。用您想要转换的内容替换SECONDS。
var time = new Date(SECONDS * 1000).toISOString().substr(11, 8);