使用PHP将youtube api返回的时间格式转换为秒 [英] Convert youtube api returned time format to seconds using PHP

查看:34
本文介绍了使用PHP将youtube api返回的时间格式转换为秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So the received duration/time format from api is this ;

PT1H1M6S

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

how can i convert this into seconds using any php function?

推荐答案

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

Here is the best solution I've encountered with Google on how to convert ISO 8601 values to seconds.

+1 表示不使用 preg 函数.我认为适合这项工作的工具.

+1 for no preg function use. Right tool for the job in my opinion.

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

Code credit to RuudBurger - copied from gist: 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

这篇关于使用PHP将youtube api返回的时间格式转换为秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆