【问题标题】:Convert seconds to Hour:Minute:Second将秒转换为时:分:秒
【发布时间】:2011-03-11 11:35:01
【问题描述】:

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

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

我怎样才能做到这一点?

【问题讨论】:

    标签: php time


    【解决方案1】:

    你可以使用gmdate()函数:

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

    【讨论】:

    • 最好确保秒数低于 86,400。
    • H 表示一天中的小时数。因此,如果您有 90000 秒并且您将在其上使用 H,则结果将是 01(第二天的第一个小时)。不是 25 - 一天只有 24 小时。
    • 我不确定这是不是正确的答案,这将产生一个datetime ...所以如果我们期望结果 > 24 小时它将不起作用。此外,如果我们需要一些负面结果(例如使用 offset ),它将不起作用。 -1 了解详情
    • 这不应该是公认的答案,因为超过 24 小时的时间存在明显缺陷。
    • 对于数字可能大于 85399 的天数,您可以使用 echo gmdate("z H:i:s", 685); z 是一年中从 0 开始的天数。您显然可以查看 php 日期手册以满足您的需求特定需求。
    【解决方案2】:

    一小时是 3600 秒,一分钟是 60 秒,为什么不呢:

    <?php
    
    $init = 685;
    $hours = floor($init / 3600);
    $minutes = floor(($init / 60) % 60);
    $seconds = $init % 60;
    
    echo "$hours:$minutes:$seconds";
    
    ?>
    

    产生:

    $ php file.php
    0:11:25
    

    (我没有测试过这么多,所以可能会出现地板错误)

    【讨论】:

    • 但他想要两个零...“00:11:25”而不是“0:11:25”
    • printf("%02d:%02d:%02d", $hours, $minutes, $seconds);
    • 好答案,但请确保在每次操作之间从 $init 中减去 $hours*3600 和 $minutes*60,否则您最终会重复计算分钟和秒数。
    • 要添加到@Amber 的评论,请使用sprintf 返回值而不是打印它。
    • 这个最适合我。补充一点。当您的时间少于 10 小时时,它会显示 3:10:59。让它看起来像 OP 想要的 03:10:59。像这样工作 $hours = floor(2300 / 3600);返回 ($hours getSeconds() % 3600);
    【解决方案3】:

    给你

    function format_time($t,$f=':') // t = seconds, f = separator 
    {
      return sprintf("%02d%s%02d%s%02d", floor($t/3600), $f, ($t/60)%60, $f, $t%60);
    }
    
    echo format_time(685); // 00:11:25
    

    【讨论】:

    • 不适用于负值。如果您的秒数为负数,请使用:return ($t&lt; 0 ? '-' : '') . sprintf("%02d%s%02d%s%02d", floor(abs($t)/3600), $f, (abs($t)/60)%60, $f, abs($t)%60); }
    【解决方案4】:

    使用函数gmdate() 仅当秒数小于86400(1天)

    $seconds = 8525;
    echo gmdate('H:i:s', $seconds);
    # 02:22:05
    

    见:gmdate()

    Run the Demo


    将秒转换为“英尺”格式无限制*

    $seconds = 8525;
    $H = floor($seconds / 3600);
    $i = ($seconds / 60) % 60;
    $s = $seconds % 60;
    echo sprintf("%02d:%02d:%02d", $H, $i, $s);
    # 02:22:05
    

    请参阅:floor()sprintf()arithmetic operators

    Run the Demo


    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
    

    见:DateTime::__construct()DateTime::modify()clonesprintf()

    Run the Demo


    MySQL示例结果的范围被限制为TIME数据类型的范围,从-838:59:59838:59:59

    SELECT SEC_TO_TIME(8525);
    # 02:22:05
    

    见:SEC_TO_TIME

    Run the Demo


    PostgreSQL 示例:

    SELECT TO_CHAR('8525 second'::interval, 'HH24:MI:SS');
    # 02:22:05
    

    Run the Demo

    【讨论】:

      【解决方案5】:

      其他解决方案使用gmdate,但在超过 86400 秒的边缘情况下会失败。为了解决这个问题,我们可以简单地自己计算小时数,然后让gmdate 将剩余的秒数计算为分钟/秒。

      echo floor($seconds / 3600) . gmdate(":i:s", $seconds % 3600);
      

      输入:6030 输出:1:40:30

      输入:2000006030 输出:555557:13:50

      【讨论】:

        【解决方案6】:
        // 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
        

        【讨论】:

        • $hms=gmdate("H:i:s",12720);获得 Day-Hour-Min-Sec 是不够的
        【解决方案7】:

        如果您不喜欢公认的答案或流行的答案,请尝试这个

        function secondsToTime($seconds_time)
        {
            if ($seconds_time < 24 * 60 * 60) {
                return gmdate('H:i:s', $seconds_time);
            } else {
                $hours = floor($seconds_time / 3600);
                $minutes = floor(($seconds_time - $hours * 3600) / 60);
                $seconds = floor($seconds_time - ($hours * 3600) - ($minutes * 60));
                return "$hours:$minutes:$seconds";
            }
        }
        
        secondsToTime(108620); // 30:10:20
        

        【讨论】:

          【解决方案8】:
          gmdate("H:i:s", no_of_seconds);
          

          如果 no_of_seconds 大于 1 天(一天中的秒数),则不会以 H:i:s 格式给出时间。
          它会忽略日值,只给出Hour:Min:Seconds

          例如:

          gmdate("H:i:s", 89922); // returns 0:58:42 not (1 Day 0:58:42) or 24:58:42
          

          【讨论】:

            【解决方案9】:

            这是一个处理负秒数和超过 1 天的秒数的单行。

            sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));
            

            例如:

            $seconds= -24*60*60 - 2*60*60 - 3*60 - 4; // minus 1 day 2 hours 3 minutes 4 seconds
            echo sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));
            

            输出:-26:03:04

            【讨论】:

            • 如果为负且少于 1 小时将不起作用:-3000 = 0:50:00 应为 -0:50:00
            【解决方案10】:

            写这样的函数来返回一个数组

            function secondsToTime($seconds) {
            
              // extract hours
              $hours = floor($seconds / (60 * 60));
            
              // extract minutes
              $divisor_for_minutes = $seconds % (60 * 60);
              $minutes = floor($divisor_for_minutes / 60);
            
              // extract the remaining seconds
              $divisor_for_seconds = $divisor_for_minutes % 60;
              $seconds = ceil($divisor_for_seconds);
            
              // return the final array
              $obj = array(
                  "h" => (int) $hours,
                  "m" => (int) $minutes,
                  "s" => (int) $seconds,
               );
            
              return $obj;
            }
            

            然后像这样简单地调用函数:

            secondsToTime(100);
            

            输出是

            Array ( [h] => 0 [m] => 1 [s] => 40 )
            

            【讨论】:

              【解决方案11】:

              见:

                  /** 
                   * Convert number of seconds into hours, minutes and seconds 
                   * and return an array containing those values 
                   * 
                   * @param integer $inputSeconds Number of seconds to parse 
                   * @return array 
                   */ 
              
                  function secondsToTime($inputSeconds) {
              
                      $secondsInAMinute = 60;
                      $secondsInAnHour  = 60 * $secondsInAMinute;
                      $secondsInADay    = 24 * $secondsInAnHour;
              
                      // extract days
                      $days = floor($inputSeconds / $secondsInADay);
              
                      // extract hours
                      $hourSeconds = $inputSeconds % $secondsInADay;
                      $hours = floor($hourSeconds / $secondsInAnHour);
              
                      // extract minutes
                      $minuteSeconds = $hourSeconds % $secondsInAnHour;
                      $minutes = floor($minuteSeconds / $secondsInAMinute);
              
                      // extract the remaining seconds
                      $remainingSeconds = $minuteSeconds % $secondsInAMinute;
                      $seconds = ceil($remainingSeconds);
              
                      // return the final array
                      $obj = array(
                          'd' => (int) $days,
                          'h' => (int) $hours,
                          'm' => (int) $minutes,
                          's' => (int) $seconds,
                      );
                      return $obj;
                  }
              

              发件人:Convert seconds into days, hours, minutes and seconds

              【讨论】:

                【解决方案12】:

                这个功能很有用,你可以扩展它:

                function formatSeconds($seconds) {
                
                if(!is_integer($seconds)) {
                    return FALSE;
                }
                
                $fmt = "";
                
                $days = floor($seconds / 86400);
                if($days) {
                    $fmt .= $days."D ";
                    $seconds %= 86400;
                }
                
                $hours = floor($seconds / 3600);
                if($hours) {
                    $fmt .= str_pad($hours, 2, '0', STR_PAD_LEFT).":";
                    $seconds %= 3600;
                }
                
                $mins = floor($seconds / 60 );
                if($mins) {
                    $fmt .= str_pad($mins, 2, '0', STR_PAD_LEFT).":";
                    $seconds %= 60;
                }
                
                $fmt .= str_pad($seconds, 2, '0', STR_PAD_LEFT);
                
                return $fmt;}
                

                【讨论】:

                  【解决方案13】:

                  试试这个:

                  date("H:i:s",-57600 + 685);
                  

                  取自
                  http://bytes.com/topic/php/answers/3917-seconds-converted-hh-mm-ss

                  【讨论】:

                  • 不完全确定,但我很确定它将时间设置为 0,然后在此之上的任何内容都只是正确的答案
                  • 这会将前导 0 放在分钟前面,您无法使用 date() 进行调整 - ca.php.net/manual/en/function.date.php
                  • @barfoon -- 是的,但我相信这是 M.Ezz 所要求的,并且它是及时使用的标准。根据我的经验,这看起来很奇怪,“3:7:5”而不是“03:07:05”,甚至“3:7”,在我看来更像是一个比例。
                  【解决方案14】:

                  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
                  

                  【讨论】:

                    【解决方案15】:

                    我已经解释过了here 也将答案粘贴在这里

                    直到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);
                    

                    将是使用的最佳选择...

                    【讨论】:

                      【解决方案16】:

                      解决方案来自: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'; }
                      

                      如果您想让代码更短(但可读性更低),您可以避免使用$converted 数组,而是将值直接放在 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"
                      

                      【讨论】:

                      • 请注意,gmdate() hack 比这更短,但仅支持最长 24 小时的持续时间。如果你使用 gmdate 技巧,任何超过 24 小时的事情都会失败!
                      【解决方案17】:

                      为此使用 DateTime 的简单方法是:

                          $time = 60; //sec.
                          $now = time();
                          $rep = new DateTime('@'.$now);
                          $diff = new DateTime('@'.($now+$time));
                          $return = $diff->diff($rep)->format($format);
                      
                          //output:  01:04:65
                      

                      这是一个简单的解决方案,让您能够使用 DateTime 的格式方法。

                      【讨论】:

                        【解决方案18】:

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

                        【讨论】:

                          【解决方案19】:
                          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);
                          }
                          

                          【讨论】:

                            【解决方案20】:

                            嗯,我需要一些东西,可以将秒减少到小时分钟和秒,但会超过 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;
                            }
                            

                            【讨论】:

                              【解决方案21】:
                              $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;
                              
                              }
                              

                              【讨论】:

                                【解决方案22】:

                                如果您需要在 javascript 中执行此操作,您可以使用此处Convert seconds to HH-MM-SS with JavaScript 回答的一行代码来执行此操作。将 SECONDS 替换为您要转换的内容。

                                var time = new Date(SECONDS * 1000).toISOString().substr(11, 8);
                                

                                【讨论】:

                                  【解决方案23】:

                                  如果你想创建一个像 YouTube 等的音频/视频时长字符串,你可以这样做:

                                  ($seconds &gt;= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds)

                                  将返回如下字符串:

                                  55.55 => '0:55'
                                  100   => '1:40'
                                  

                                  在 >= 24 小时内可能无法正常工作。

                                  【讨论】:

                                    【解决方案24】:

                                    这是一个很好的方法:

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

                                    【讨论】:

                                      【解决方案25】:

                                      以下代码可以准确显示总小时加上分钟和秒

                                      $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 准则,不应该回答。重复的问题应该通过 cmets 重定向,或者通过引用已经提出的问题来结束
                                      【解决方案26】:

                                      以防万一其他人正在寻找一个简单的函数来返回这个格式很好的(我知道这不是 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;
                                      }
                                      

                                      【讨论】:

                                        【解决方案27】:

                                        任何人在未来寻找这个,这给出了最初的海报要求的格式。

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

                                        【讨论】:

                                          【解决方案28】:
                                          <?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, ";
                                          }
                                          

                                          【讨论】:

                                            猜你喜欢
                                            • 2015-02-03
                                            • 2021-10-29
                                            • 2011-06-09
                                            • 1970-01-01
                                            • 1970-01-01
                                            • 2015-03-01
                                            • 1970-01-01
                                            相关资源
                                            最近更新 更多