1

I need to get the video duration using Youtube API V3. My application was working fine with API V3 but now it doesn't work.

I found a working example and it works:

$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'];
}

Credits: Youtube API v3 , how to get video durations?

This will return the time format something like this.

PT24M30S 

How can I convent this to a readable time. Like 24:30?

Community
  • 1
  • 1
nick cruse
  • 141
  • 2
  • 5
  • 15

6 Answers6

6

I can't believe it, anyone have used DateInterval, why? It's just like this:

$duration = new DateInterval('PT24M30S');

print $duration->format('%H:%i:%s'); // outputs: 00:24:30

Helpful links:

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
  • This is the best answer! If using laravel don't forget to import the class into the controller or whatever you are using it: `use DateInterval;` – Crysfel Dec 29 '21 at 22:01
3

One possible way is to use the function str_replace();

$stamp = "PT24M30S";
$formated_stamp = str_replace(array("PT","M","S"), array("",":",""),$stamp);

echo $formated_stamp; //should give "24:30"

Bonus content - leading zeros

In order to add leading zeros one must first split the string up with explode(), then format the numbers idividually with sprintf(); and finally add it all together again.

$exploded_string = explode(":",$formated_stamp);
$new_formated_stamp = sprintf("%02d", $exploded_string[0]).":".sprintf("%02d", $exploded_string[1]);
Niddro
  • 1,705
  • 2
  • 12
  • 24
1
 $time_format = "PT24M30S ";

preg_match_all('/(\d+)/',$time_format,$parts);

$hours = floor($parts[0][0]/60);
$minutes = $parts[0][0]%60;
$seconds = $parts[0][1];

echo $hours . ": ". $minutes .":". $seconds; 
monirz
  • 534
  • 5
  • 14
0

video resource contains a duration field which is a string of the following format. It will be up to you the developer to reformat it as necessary for your application and needs.

contentDetails.duration

The length of the video. The tag value is an ISO 8601 duration. For example, for a video that is at least one minute long and less than one hour long, the duration is in the format PT#M#S, in which the letters PT indicate that the value specifies a period of time, and the letters M and S refer to length in minutes and seconds, respectively. The # characters preceding the M and S letters are both integers that specify the number of minutes (or seconds) of the video. For example, a value of PT15M33S indicates that the video is 15 minutes and 33 seconds long.

If the video is at least one hour long, the duration is in the format PT#H#M#S, in which the # preceding the letter H specifies the length of the video in hours and all of the other details are the same as described above. If the video is at least one day long, the letters P and T are separated, and the value's format is P#DT#H#M#S. Please refer to the ISO 8601 specification for complete details.

As to how to do it I would have to say remove the PT and the S and replace the M with a :. It will probably require some testing on your part depending on what happens when a value is null. I would do some research into ISO-8061

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

I use the following function to convert the YouTube duration. Simply replace $yt with the format YouTube provided you and it'll print it out nice and readable.

function ConvertYoutubeVideoDuration($yt){
$yt=str_replace(['P','T'],'',$yt);
foreach(['D','H','M','S'] as $a){
    $pos=strpos($yt,$a);
    if($pos!==false) ${$a}=substr($yt,0,$pos); else { ${$a}=0; continue; }
    $yt=substr($yt,$pos+1);
}
if($D>0){
    $M=str_pad($M,2,'0',STR_PAD_LEFT);
    $S=str_pad($S,2,'0',STR_PAD_LEFT);
    return ($H+(24*$D)).":$M:$S"; // add days to hours
} elseif($H>0){
    $M=str_pad($M,2,'0',STR_PAD_LEFT);
    $S=str_pad($S,2,'0',STR_PAD_LEFT);
    return "$H:$M:$S";
} else {
    $S=str_pad($S,2,'0',STR_PAD_LEFT);
    return "$M:$S";
}
}
Born2DoubleUp
  • 109
  • 1
  • 10
0

Solution PHP

function covtime($youtube_time){
            preg_match_all('/(\d+)/',$youtube_time,$parts);
            $hours = $parts[0][0];
            $minutes = $parts[0][1];
            $seconds = $parts[0][2];
            if($seconds != 0)
                return $hours.':'.$minutes.':'.$seconds;
            else
                return $hours.':'.$minutes;
        }
Shahzaib Chadhar
  • 178
  • 1
  • 10