使用PHP将youtube api返回的时间格式转换为秒

Sha*_*rif 5 php time youtube-api

所以从api接收到的持续时间/时间格式是这样的;

PT1H1M6S
Run Code Online (Sandbox Code Playgroud)

如何使用任何 php 函数将其转换为秒?

fic*_*scr 9

这是我在 Google 遇到的关于如何将 ISO 8601 值转换为秒的最佳解决方案。

+1 表示不使用 preg 功能。在我看来,这项工作的正确工具。

代码归功于 RuudBurger - 从要点复制:https ://gist.github.com/w0rldart/9e10aedd1ee55fc4bc74

/**
 * Convert ISO 8601 values like P2DT15M33S
 * to a total value of seconds.
 *
 * @param string $ISO8601
 */
function ISO8601ToSeconds($ISO8601){
    $interval = new \DateInterval($ISO8601);

    return ($interval->d * 24 * 60 * 60) +
        ($interval->h * 60 * 60) +
        ($interval->i * 60) +
        $interval->s;
}

echo ISO8601ToSeconds('P20DT15M33S'); // Returns a value of 1728933
Run Code Online (Sandbox Code Playgroud)