在youtube数据API中,我得到响应json,如下所示:
将PT1M20S转换为秒......
{
"items": [
{
"kind": "youtube#video",
"etag": "\"jOXstHOM20qemPbHbyzf7ztZ7rI/nqqiS3qfiG_sVIXsu6EXwft5FcI\"",
"id": "cIHdPva1Wdg",
"contentDetails": {
"duration": "PT1M20S",
"dimension": "2d",
"definition": "sd",
"caption": "false",
"licensedContent": false
}
}
]
}
我需要将持续时间转换为秒来显示其长度。任何DateTime方式我都可以做到。或者我需要使用正则表达式。
答案 0 :(得分:2)
您可以创建一个新的DateInterval
对象,然后只需以秒为单位计算间隔,例如
$di = new DateInterval("PT1M20S");
echo ($di->days * 3600 * 24) + ($di->h * 3600) + ($di->i * 60) + $di->s;
答案 1 :(得分:0)
<?php
$duration = 'PT1M28S';
echo durationToSeconds($duration);
function durationToSeconds($duration)
{
$date = new DateTime;
$current = $date->getTimestamp();
$date->add(new DateInterval($duration));
return $date->getTimestamp() - $current;
}