Converting youtube duration time...

I didn’t write the ‘conversion’ part of this… no sense in reinventing the wheel… but I am trying to adapt for my uses and am getting a blank screen when I run it… I have no idea what’s wrong with it… any help would be appreciated!!!

[php]

<?php $dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=Um3f4BJwCc4&key=AIzaSyBw1asGhkxHxFf6JqyNrTqIeJ9sjMKFcX4"); $dury =json_decode($dur, true); foreach ($dury['items'] as $d) { $duration= $d['contentDetails']['duration']; echo "
";
print_r($duration);
  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];
    }
    }
    }
?>

[/php]

How do I actually print out the return?

Thanks!!

Have you double checked the that the results that you are receiving are in the format you are expecting?

There is probably a key, but the results I get on that url are:

{
“error”: {
“errors”: [
{
“domain”: “usageLimits”,
“reason”: “keyInvalid”,
“message”: “Bad Request”
}
],
“code”: 400,
“message”: “Bad Request”
}
}

[php]function duration($ytDuration) {
$di = new DateInterval($ytDuration);

return array(
	'hours' => $di->h,
	'minutes' => $di->i,
	'seconds' => $di->s
);

}

// @TODO cache this so we don’t hit googleapis n times a day!
$json = file_get_contents(‘https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=Um3f4BJwCc4&key=AIzaSyBw1asGhkxHxFf6JqyNrTqIeJ9sjMKFcX4’);

$data = json_decode($json);

foreach ($data->items as $item) {
$duration = duration($item->contentDetails->duration);
echo '

ID: ’ . $item->id . ‘

’;
echo 'Hours: ’ . $duration[‘hours’] . ’ | Minutes: ’ . $duration[‘minutes’] . ’ | Seconds: ’ . $duration[‘seconds’];
}[/php]

Shamelessly stole the function from here: http://stackoverflow.com/a/25871704/1078488
Did some changes

No shame in that… shouldn’t reinvent the wheel just because LOL

Thanks!!

Have you double checked the that the results that you are receiving are in the format you are expecting?

There is probably a key, but the results I get on that url are:

{
“error”: {
“errors”: [
{
“domain”: “usageLimits”,
“reason”: “keyInvalid”,
“message”: “Bad Request”
}
],
“code”: 400,
“message”: “Bad Request”
}
}

That’s because it wasn’t running from my domain… sorry. I should have checked that…ugh… rookie mistakes LOL

Thank you!

// @TODO cache this so we don't hit googleapis n times a day! $json = file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=Um3f4BJwCc4&key=AIzaSyBw1asGhkxHxFf6JqyNrTqIeJ9sjMKFcX4');

$data = json_decode($json);

YES on the agenda todo!! Ultimately I would like to write this to an xml file and only check it x times a day rather then everytime someone loads the page :wink:

I wpuld suggest using json, personal preference but also since you are already using json it makes sense to use it for that as well.

Also cache to disk leads to you actually having to read that file (which is slow). Check if you have abn in mremory caches available (apc, memcache(d)., etc)

Yeah Thanks for all the help but I’m just going to scrap it for now…

Google’s Youtube api V3 is a pain in the butt… I have to make two different calls to get the info… can’t do it in one anymore. Even the hardcore programmers are not happy about it… It’s been a reported issue and is on Google’s ‘wontfix’ list as outlined in their forums… makes no sense but it’s all about money… 2 calls equals 4 points… and you only get so many per day so if you have a ton of users you’re racking up the points…

BUT I cannot get the format to show up… like I said I can get the PT4H23M32S but can’t get it formatted to show correctly… I have found a few functions to do it but haven’t been able to get one to work yet… frustrating.

Jim yeah it wasn’t working because you actually have to use it all for it to work LOL WOW I think I’ve lost it… yes it works!! I will play with it so it only shows Hour if there’s actually one there… :slight_smile:

I agree but I am basically building this like in ‘modules’… or stages… getting one thing to work then moving on… at some point I will post the entire code here and let people either use it or improve it…

I will ask about mem cache… it’s GoDaddy they are pretty up-to-date on things so I’m guessing yes and I did read where writing to a file slows things down now that I think about it…

There all modified so it looks like this:

“2726 views/ Time: Hours: 1 | Minutes: 8 | Seconds: 37”

or

“5096 views/ Time: Minutes: 3 | Seconds: 49”

Thanks again Jim!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service