在 PHP 中将 Youtube Data API V3 视频时长格式转换为标准时间 [英] Converting Youtube Data API V3 video duration format to standard time in PHP

查看:45
本文介绍了在 PHP 中将 Youtube Data API V3 视频时长格式转换为标准时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从 youtube API 请求中获取此数组,但我认为持续时间格式非常罕见.他们为什么不直接在那里投入几秒钟?无论如何,这是数组

I'm, getting this array from a youtube API request, but the duration format is very rare in my opinion. Why didn't they just throw seconds in there? In any case this is the array

[duration] => PT2M3S
[dimension] => 2d
[definition] => sd
[caption] => false

有没有办法在 PHP 中将此持续时间转换为H:i:s"格式?

Is there a way to convert this duration to a "H:i:s" format in PHP?

预先感谢您的帮助

推荐答案

其实就是简单的ISO8601日期.使用正则表达式(如 (\d+) )将字符串拆分为分钟和秒.然后将分钟除以 60 的整数部分得到小时.获取该分区的剩余部分以获得分钟.

In fact it is simple ISO8601 date. Split string with regexp like (\d+) to minutes and seconds. Then get integer part of division of minutes by 60 to get hours. Get remainder of that division to get minutes.

这是一个应该可以工作的例子,虽然我没有测试过

here is an example that should work though i didn't tested it

preg_match_all('/(\d+)/',$youtube_time,$parts);

$hours = floor($parts[0][0]/60);
$minutes = $parts[0][0]%60;
$seconds = $parts[0][1];

这篇关于在 PHP 中将 Youtube Data API V3 视频时长格式转换为标准时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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