Skip to content

Instantly share code, notes, and snippets.

@mucahit
Created July 7, 2015 16:03
Show Gist options
  • Save mucahit/408af990d58f309b0a23 to your computer and use it in GitHub Desktop.
Save mucahit/408af990d58f309b0a23 to your computer and use it in GitHub Desktop.
Youtube Api Duration to Seconds
function parse_duration($duration, $resultType){
$matches = array();
preg_match('/^(-|)?P([0-9]+Y|)?([0-9]+M|)?([0-9]+D|)?T?([0-9]+H|)?([0-9]+M|)?([0-9]+S|)?$/', $duration, $matches);
if(!empty($matches)){
foreach($matches as &$match){
$match = preg_replace('/((?!([0-9]|-)).)*/', '', $match);
}
switch ($resultType) {
case 'second':
return ($matches[6]*60)+$matches[7];
break;
case 'minute':
return $matches[6].':'.$matches[7];
break;
}
}
else{
return false;
}
}
print_r(parse_duration('PT18M21S','second'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment