我需要使用Youtube API V3获取视频时长。我的应用程序可以在API V3上正常工作,但现在不起作用。
我找到了一个可行的示例,它的工作原理是:
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=[VIDOID]&key=[API KEY]");
$duration =json_decode($dur, true);
foreach ($duration['items'] as $vidTime) {
$vTime= $vidTime['contentDetails']['duration'];
}
积分:Youtube API v3 , how to get video durations?
这将返回类似这样的时间格式。
PT24M30S
我如何才能将其节省到可读的时间。喜欢24:30?
最佳答案
一种可能的方法是使用函数str_replace();
$stamp = "PT24M30S";
$formated_stamp = str_replace(array("PT","M","S"), array("",":",""),$stamp);
echo $formated_stamp; //should give "24:30"
红利内容-前导零
为了添加前导零,必须首先使用explode()将字符串分割开,然后使用sprintf()单独格式化数字;最后将它们重新添加在一起。
$exploded_string = explode(":",$formated_stamp);
$new_formated_stamp = sprintf("%02d", $exploded_string[0]).":".sprintf("%02d", $exploded_string[1]);
关于php - 使用YouTube API获取持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31161896/