【问题标题】:Converting Youtube Data API V3 video duration format to standard time in PHP在 PHP 中将 Youtube Data API V3 视频持续时间格式转换为标准时间
【发布时间】:2013-11-02 22:33:22
【问题描述】:

我是从 youtube API 请求中获取此数组,但在我看来,持续时间格式非常罕见。他们为什么不直接扔几秒钟呢?无论如何这是数组

[duration] => PT2M3S
[dimension] => 2d
[definition] => sd
[caption] => false

有没有办法在 PHP 中将此持续时间转换为“H:i:s”格式?

提前感谢您的帮助

【问题讨论】:

    标签: php youtube-data-api


    【解决方案1】:

    acidjazz 有一个很好的解决方案,但是在显示分钟的地方有一个错字。它应该是 $di->i 分钟,而不是 $di->m。如果您没有在对象中使用它,您也可以删除“公共静态”部分,我取出了sprintf()

    否则函数按原样工作:

    function duration($ytDuration) {
    
        $di = new DateInterval($ytDuration);
        $string = '';
    
        if ($di->h > 0) {
          $string .= $di->h.':';
        }
    
        return $string.$di->i.':'.$di->s;
    }
    

    我会将此作为评论添加到他的回答中,但是整个“你需要 50 声望才能评论”BS 阻止了我坚持这个线程的流程。

    【讨论】:

    • 是的,结果是正确的。但是,就我而言。我总是在所有小时、分钟、秒内使用 sprintf('%02s', 'xx') 。 (xx 表示那些单位)。如果没有前导零,则结果可能是 1:2:6 持续 1 小时 2 分 6 秒。 1:2:6 看起来很奇怪。谢谢你的回答。
    【解决方案2】:

    其实就是简单的 ISO8601 日期。使用正则表达式(如(\d+))将字符串拆分为分钟和秒。然后将分钟除以 60 的整数部分得到小时。获得该部门的其余部分以获得分钟。

    这是一个虽然我没有测试过但应该可以工作的例子

    preg_match_all('/(\d+)/',$youtube_time,$parts);
    
    $hours = floor($parts[0][0]/60);
    $minutes = $parts[0][0]%60;
    $seconds = $parts[0][1];
    

    【讨论】:

    • 感谢您的方法,最后我做到了。 code $duration = new DateInterval($videoData['duration']); $videoDuration = (60 * 60 * $duration->h) + (60 * $duration->i) + $duration->s;打印 gmdate("H:i:s", $videoDuration);
    • 如果你有这样的模式,这将得到错误的数据:PT54S,如果视频少于 1 分钟,youtube 会给你这个持续时间
    【解决方案3】:

    您可以使用 php 的 DateInterval 类来正确解析它。例如,youtube 包装类中的函数将其解析为 YouTube 显示的格式可能如下所示:

      public static function duration($ytDuration) {
    
        $di = new DateInterval($ytDuration);
        $string = '';
    
        if ($di->h > 0) {
          $string .= $di->h.':';
        }
    
        return $string.$di->m.':'.sprintf('%02s',$di->s);
      }
    

    【讨论】:

      【解决方案4】:

      我使用了@chris-z-s 的答案here,并将其转换为php:

      function getDurationSeconds($duration){
          preg_match_all('/[0-9]+[HMS]/',$duration,$matches);
          $duration=0;
          foreach($matches as $match){
              //echo '<br> ========= <br>';       
              //print_r($match);      
              foreach($match as $portion){        
                  $unite=substr($portion,strlen($portion)-1);
                  switch($unite){
                      case 'H':{  
                          $duration +=    substr($portion,0,strlen($portion)-1)*60*60;            
                      }break;             
                      case 'M':{                  
                          $duration +=substr($portion,0,strlen($portion)-1)*60;           
                      }break;             
                      case 'S':{                  
                          $duration +=    substr($portion,0,strlen($portion)-1);          
                      }break;
                  }
              }
          //  echo '<br> duratrion : '.$duration;
          //echo '<br> ========= <br>';
          }
           return $duration;
      
      }
      

      它对我有用 :)

      【讨论】:

        【解决方案5】:

        可能对某人获得持续时间有所帮助:

        $interval = new \DateInterval($iso8601);
        
        // in seconds:
        $seconds = $interval->h * 360 + $interval->i * 60 + $interval->s;
        
        // in "H:i:s" format:
        echo $interval->format('%H:%i:%s');
        

        【讨论】:

          【解决方案6】:

          这是我的解决方案:

          preg_match('/(\d+)H/', $duration, $match);
          $h = ($match[0])?filter_var($match[0], FILTER_SANITIZE_NUMBER_INT).":":'';
          preg_match('/(\d+)M/', $duration, $match);
          $m = filter_var($match[0], FILTER_SANITIZE_NUMBER_INT).":";
          preg_match('/(\d+)S/', $youtube_date, $match);
          $s = filter_var($match[0], FILTER_SANITIZE_NUMBER_INT);
          echo: "Duration: " . $h.$m.$s;
          

          【讨论】:

            【解决方案7】:

            ##:##:## PHP 上的时间格式

             function NormalizeDuration($duration){
                preg_match('#PT(.*?)H(.*?)M(.*?)S#si',$duration,$out);
                if(empty($out[1])){
                preg_match('#PT(.*?)M(.*?)S#si',$duration,$out);
                if(empty($out[1])){
                preg_match('#PT(.*?)S#si',$duration,$out);
                if(empty($out[1])){
                return '00:00';
                }
                }else{
                if(strlen($out[1])==1){ $out[1]= '0'.$out[1]; }
                if(strlen($out[2])==1){ $out[2]= '0'.$out[2]; }
                return $out[1].':'.$out[2];
                }
                }else{
                if(strlen($out[1])==1){ $out[1]= '0'.$out[1]; }
                if(strlen($out[2])==1){ $out[2]= '0'.$out[2]; }
                if(strlen($out[3])==1){ $out[3]= '0'.$out[3]; }
                return $out[1].':'.$out[2].':'.$out[3];
                }
                }
            

            【讨论】:

              【解决方案8】:

              使用以下 php 函数将返回 YouTube 视频时长。更多精彩代码请关注linkmore code samples

                  function getYoutubeDuration($vid) 
                  {
                  $videoDetails =file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$vid."&part=contentDetails&key=AIzaSyDen99uTRRPl9VT_Ra9eAisvZSc4aZDEns");
                  $videoDetails =json_decode($videoDetails, true);
                  foreach ($videoDetails['items'] as $vidTime)
                   {
                      $youtube_time = $vidTime['contentDetails']['duration'];
                      preg_match_all('/(\d+)/',$youtube_time,$parts);
                      $hours = (strlen(floor($parts[0][0]/60))<1)?floor($parts[0][0]/60):'0'.floor($parts[0][0]/60);
                      $minutes = $parts[0][0]%60; (strlen($parts[0][0]%60)<1)?$parts[0][0]%60:'0'.$parts[0][0]%60;
                      $seconds = $parts[0][1];(strlen($parts[0][1])<1)?$parts[0][1]:'0'.$parts[0][1];
                      return $hours.":".$minutes.":".$seconds;
                   }
                  }
              
                echo getYoutubeDuration('J1lb3qJgfho');
              

              【讨论】:

                【解决方案9】:

                由于 YouTube (Google) 喜欢困难,他们选择了非标准格式。 所以标准函数并不总是有效。

                因此,我根据需要制作了这个。

                $length = $data['items'] ..... ['duration'];
                
                $seconds = substr(stristr($length, 'S', true), -2, 2);
                $seconds = preg_replace("/[^0-9]/", '', $seconds);
                $minutes =  substr(stristr($length, 'M', true), -2, 2);
                $minutes = preg_replace("/[^0-9]/", '', $minutes);
                $hours =  substr(stristr($length, 'H', true), -2, 2);
                $hours = preg_replace("/[^0-9]/", '', $hours);
                

                【讨论】:

                  【解决方案10】:

                  使用命名组的解决方案

                  PT((?P<hours>\d+)H)?((?P<minutes>\d+)M)?((?P<seconds>\d+)S)?
                  

                  可以在这里找到示例https://regex101.com/r/wU3hT2/114

                  Python 函数:

                  def yt_time_to_seconds(time):
                      regex_string = 'PT((?P<hours>\d+)H)?((?P<minutes>\d+)M)?((?P<seconds>\d+)S)?'
                      regex = re.compile(regex_string)
                      match = regex.match(time)
                      if match:
                          hours = int(match.group('hours'))
                          minutes = int(match.group('minutes'))
                          seconds = int(match.group('seconds'))
                          return hours * 60 * 60 + minutes * 60 + seconds
                      return 0
                  

                  【讨论】:

                    【解决方案11】:

                    它对我来说 100% 完美,只有你应该拥有 \DateInterval 作为根

                    function NormalizeDuration($duration){ //pass your time
                        $start = new \DateTime('@0');
                        $start->add(new \DateInterval($duration));
                    
                        return $start->format('H:i:s'); //format
                    }
                    

                    【讨论】:

                      猜你喜欢
                      • 2021-08-05
                      • 1970-01-01
                      • 2014-04-04
                      • 2014-08-15
                      • 2015-08-24
                      • 2013-05-24
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多