【问题标题】:Output is in seconds. convert to hh:mm:ss format in php输出以秒为单位。在 php 中转换为 hh:mm:ss 格式
【发布时间】:2011-04-01 20:42:06
【问题描述】:
  1. 我的输出格式为 290.52262423327 秒。如何将其更改为 00:04:51?

  2. 我想以秒和 HH:MM:SS 格式显示相同的输出,所以如果是秒,我只想显示 290.52 秒。(小数点后只有两个整数)?我该怎么做?

我正在使用 php,输出存在于 $time 变量中。想将这个 $time 更改为 $newtime,HH:MM:SS 和 $newsec 为 290.52。

谢谢:)

【问题讨论】:

标签: php time


【解决方案1】:

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

【讨论】:

  • 我遇到了一些极端不成功的除法和减法函数,试图实现你在那一行中所做的事情:) sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60)
【解决方案2】:

试试这个

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

【讨论】:

  • 此解决方案适用于小于一天中秒数的任何值,但对于 ≥ 86400 的值将重置为 00:00:00。
  • 关于如何使其适用于 >86400 的值的任何想法??
  • 这是误导性的:gmdate() 接受 Unix 时间戳作为第二个参数,因此实际上您在 1970 年 1 月 1 日 00:00:00 GMT 90 秒后得到“H:i:s”部分,当然,如果您超过一天中的秒数(86400),您将获得第二天的“H:i:s”部分。仅在保证您始终拥有少于一天的秒数时才使用此选项。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:
    $iSeconds = 290.52262423327;
    print date('H:i:s', mktime(0, 0, $iSeconds));
    

    【讨论】:

    • 如果您的$iSeconds 超过 86399(一天中的秒数),这个答案也会有问题。 86401 会给你 00:00:01。
    【解决方案5】:

    试试这个:

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

    【讨论】:

      【解决方案6】:

      我不知道这是否是最有效的方式,但如果您还需要显示天数,这可行:

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

      【讨论】:

        【解决方案7】:

        试试这个:)

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

        【讨论】:

          【解决方案8】:

          就个人而言,我根据其他人的答案制作了自己的解析器。 适用于天、小时、分钟和秒。 应该很容易扩展到几周/几个月等。 它也适用于 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);
          }
          

          【讨论】:

            【解决方案9】:

            基于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
              );
            }
            

            【讨论】:

              【解决方案10】:

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

              Numero 二人组...echo round(290.52262423327,2);

              【讨论】:

              • 您发布的链接已失效
              【解决方案11】:

              1)

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

              2)

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

              【讨论】:

                【解决方案12】:

                如果你使用的是 Carbon(比如在 Laravel 中),你可以这样做:

                $timeFormatted = \Carbon\Carbon::now()->startOfDay()->addSeconds($seconds)->toTimeString();

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

                请看caveats

                【讨论】:

                  【解决方案13】:

                  这是我的微秒实现

                      /**
                       * @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
                          );
                      }
                  

                  【讨论】:

                    【解决方案14】:
                    echo date('H:i:s',$time);
                    
                    echo number_format($time,2);
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2012-02-10
                      • 1970-01-01
                      • 1970-01-01
                      • 2022-11-28
                      • 2018-11-29
                      • 1970-01-01
                      • 2019-10-28
                      • 2012-02-20
                      相关资源
                      最近更新 更多