4

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

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

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

在此先感谢您的帮助

4

11 回答 11

7

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 阻止了我坚持这个线程的流程。

于 2014-09-16T14:39:35.563 回答
5

事实上它是简单的 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];
于 2013-10-24T10:00:25.550 回答
4

您可以使用 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);
  }
于 2014-01-24T22:53:57.407 回答
3

我在这里使用了@chris-zs 的答案,并将其转换为 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;

}

它对我有用:)

于 2014-10-03T12:26:37.383 回答
1

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

$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');
于 2016-12-06T12:45:48.957 回答
0

##:##:## 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];
    }
    }
于 2013-12-03T22:39:23.963 回答
0

这是我的解决方案:

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;
于 2013-11-20T17:31:46.493 回答
0

使用以下 php 函数将返回 YouTube 视频时长。更多精彩代码请点击此链接 更多代码示例

    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');
于 2015-07-16T14:13:51.103 回答
0

由于 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);
于 2017-02-13T12:59:57.470 回答
0

具有命名组的解决方案

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
于 2018-07-16T11:10:50.493 回答
0

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

function NormalizeDuration($duration){ //pass your time
    $start = new \DateTime('@0');
    $start->add(new \DateInterval($duration));

    return $start->format('H:i:s'); //format
}
于 2018-10-05T10:13:00.763 回答