Created
July 7, 2015 16:03
-
-
Save mucahit/408af990d58f309b0a23 to your computer and use it in GitHub Desktop.
Youtube Api Duration to Seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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