Created
September 12, 2015 12:31
-
-
Save w0rldart/9e10aedd1ee55fc4bc74 to your computer and use it in GitHub Desktop.
ISO 8601 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
/** | |
* Convert ISO 8601 values like PT15M33S | |
* to a total value of seconds. | |
* | |
* @param string $ISO8601 | |
*/ | |
function ISO8601ToSeconds($ISO8601) | |
{ | |
preg_match('/\d{1,2}[H]/', $ISO8601, $hours); | |
preg_match('/\d{1,2}[M]/', $ISO8601, $minutes); | |
preg_match('/\d{1,2}[S]/', $ISO8601, $seconds); | |
$duration = [ | |
'hours' => $hours ? $hours[0] : 0, | |
'minutes' => $minutes ? $minutes[0] : 0, | |
'seconds' => $seconds ? $seconds[0] : 0, | |
]; | |
$hours = substr($duration['hours'], 0, -1); | |
$minutes = substr($duration['minutes'], 0, -1); | |
$seconds = substr($duration['seconds'], 0, -1); | |
$toltalSeconds = ($hours * 60 * 60) + ($minutes * 60) + $seconds; | |
return $toltalSeconds; | |
} | |
echo ISO8601ToSeconds('PT15M33S'); // Returns a value of 933 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good solution RuudBuger. Refrenced that code here: https://stackoverflow.com/questions/44573677/convert-youtube-api-returned-time-format-to-seconds-using-php/44573805#44573805 and credited you.