将秒转换为时:分:秒

394

我需要将秒转换为“时:分:秒”格式。

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

我该如何实现?

30个回答

0
function timeToSecond($time){
    $time_parts=explode(":",$time);
    $seconds= ($time_parts[0]*86400) + ($time_parts[1]*3600) + ($time_parts[2]*60) + $time_parts[3] ; 
    return $seconds;
}

function secondToTime($time){
    $seconds  = $time % 60;
    $seconds<10 ? "0".$seconds : $seconds;
    if($seconds<10) {
        $seconds="0".$seconds;
    }
    $time     = ($time - $seconds) / 60;
    $minutes  = $time % 60;
    if($minutes<10) {
        $minutes="0".$minutes;
    }
    $time     = ($time - $minutes) / 60;
    $hours    = $time % 24;
    if($hours<10) {
        $hours="0".$hours;
    }
    $days     = ($time - $hours) / 24;
    if($days<10) {
        $days="0".$days;
    }

    $time_arr = array($days,$hours,$minutes,$seconds);
    return implode(":",$time_arr);
}

0

这是一种漂亮的方法来实现那个:

function time_converter($sec_time, $format='h:m:s'){
      $hour = intval($sec_time / 3600) >= 10 ? intval($sec_time / 3600) : '0'.intval($sec_time / 3600);
      $minute = intval(($sec_time % 3600) / 60) >= 10 ? intval(($sec_time % 3600) / 60) : '0'.intval(($sec_time % 3600) / 60);
      $sec = intval(($sec_time % 3600) % 60)  >= 10 ? intval(($sec_time % 3600) % 60) : '0'.intval(($sec_time % 3600) % 60);

      $format = str_replace('h', $hour, $format);
      $format = str_replace('m', $minute, $format);
      $format = str_replace('s', $sec, $format);

      return $format;
    }

0
如果您需要在JavaScript中完成此操作,只需一行代码即可,如使用JavaScript将秒转换为HH-MM-SS所述。将 SECONDS 替换为要转换的内容即可。
var time = new Date(SECONDS * 1000).toISOString().substr(11, 8);

0

我需要一个能够将秒数转换为小时、分钟和秒的函数,但是它应该超过24小时,并且不会进一步减少到天数。

这里有一个简单的函数可以实现。你可能可以改进它...但是这就是它:

function formatSeconds($seconds)
{
    $hours = 0;$minutes = 0;
    while($seconds >= 60){$seconds -= 60;$minutes++;}
    while($minutes >= 60){$minutes -=60;$hours++;}
    $hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
    $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT);
    $seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);
    return $hours.":".$minutes.":".$seconds;
}

0
$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;

}

0

在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;
}

0

以下代码可以准确地显示总小时数、分钟数和秒数

$duration_in_seconds = 86401;
if($duration_in_seconds>0)
{
    echo floor($duration_in_seconds/3600).gmdate(":i:s", $duration_in_seconds%3600);
}
else
{
    echo "00:00:00";
}

在SO上,仅提供代码的答案是不被鼓励的。请添加解释以增加长期价值。未来的访问者应该能够从您的答案中学习并将这些知识应用到自己的问题中。这样的答案通常被认为质量更高(有利于平台),也更有可能被点赞。请考虑编辑以添加上下文或突出重要部分。请在以后的回答中考虑这一点。最后,有些问题不符合SO的指南,不应该被回答。重复的问题应该通过评论或关闭并引用已经提出的问题来重定向。 - SherylHohman

-1

如果有其他人正在寻找一个简单的函数来返回这个漂亮格式化的内容(我知道这不是OP要求的格式),这就是我刚想出来的。感谢 @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;
}

-1
任何未来寻找此内容的人,这提供了最初发布者所要求的格式。
$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";

-3
<?php
$time=3*3600 + 30*60;


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

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

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

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

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

$second=floor($time);
$time-=$second;
if($year>0){
    echo $year." year, ";
}
if($month>0){
    echo $month." month, ";
}
if($day>0){
    echo $day." day, ";
}
if($hour>0){
    echo $hour." hour, ";
}
if($minute>0){
    echo $minute." minute, ";
}
if($second>0){
    echo $second." second, ";
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接