Skip to content

Instantly share code, notes, and snippets.

@webinista
Created December 13, 2018 03:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webinista/08a03133759bb3e6acbfa1d900c0f93d to your computer and use it in GitHub Desktop.
Save webinista/08a03133759bb3e6acbfa1d900c0f93d to your computer and use it in GitHub Desktop.
PHP: Convert an ISO 8601 duration / date (Such as the one YouTube uses) interval to seconds
<?php
function iso8601ToSeconds($input) {
$duration = new DateInterval($input);
$hours_to_seconds = $duration->h * 60 * 60;
$minutes_to_seconds = $duration->i * 60;
$seconds = $duration->s;
return $hours_to_seconds + $minutes_to_seconds + $seconds;
}
@alex8bitw
Copy link

This doesn't work.

PHP Fatal error:  Uncaught Exception: Unknown or bad format (P10675199DT2H48M5.4775807S) in /tmp/t.php:4
Stack trace:
#0 /tmp/t.php(4): DateInterval->__construct()
#1 /tmp/t.php(11): iso8601ToSeconds()
#2 {main}
  thrown in /tmp/t.php on line 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment