0

Currently when you get youtube duration you get something like PT3M10S.

The code below changes that into 3:10. However I'm looking to only get SECONDS so 190 would be the output.

$duration = str_replace(array("PT","M","S"), array("",":",""),$duration);
Ivan Gabriele
  • 6,433
  • 5
  • 39
  • 60
UKTSLiam
  • 29
  • 5

4 Answers4

2

You could try using a regular expression to get the values out, and then get the total seconds from there. Something like:

$duration="PT3M10S";
$pattern='/PT(\d+)M(\d+)S/';
preg_match($pattern,$duration,$matches);
$seconds=$matches[1]*60+$matches[2];

The pattern puts the minutes into the first group and the seconds into the next. $matches will contain the matched groups after calling preg_match(). You'll need to get groups 1 and 2, as group 0 in the match is the full matched text. In this case "PT3M10S".

sbrass
  • 905
  • 7
  • 12
  • I get _Undefined offset_ errors. Check [my answer](https://stackoverflow.com/a/48745771/782013). – gordie Feb 12 '18 at 11:47
1

Seen here :

    $youtube_duration = $vinfo['duration'];
    $interval = new DateInterval(youtube_duration);
    $seconds = $interval->h * 3600 + $interval->i * 60 + $interval->s;
gordie
  • 1,637
  • 3
  • 21
  • 41
0
function covtime($youtube_time) {
    preg_match_all('/(\d+)/', $youtube_time, $parts);
    $h = preg_match('/H/', $youtube_time);
    $m = preg_match('/M/', $youtube_time);
    $s = preg_match('/S/', $youtube_time);
    if ($h && $m && $s) {
        $hours = $parts[0][0];
        $minutes = $parts[0][1];
        $seconds = $parts[0][2];
        if ($minutes < 10) {
            $minutes = '0' . $parts[0][1];
        }
        if ($seconds < 10) {
            $seconds = '0' . $parts[0][2];
        }
        echo $hours . ':' . $minutes . ':' . $seconds;
    } else if ($h && $m) {
        $hours = $parts[0][0];
        $minutes = $parts[0][1];
        $seconds = '00';
        if ($minutes < 10) {
            $minutes = '0' . $parts[0][1];
        }
        echo $hours . ':' . $minutes . ':' . $seconds;
    } else if ($m && $s) {
        $minutes = $parts[0][0];
        $seconds = $parts[0][1];
        if ($seconds < 10) {
            $seconds = '0' . $parts[0][1];
        }
        echo $minutes . ':' . $seconds;
    } else if ($h && $s) {
        $hours = $parts[0][0];
        $minutes = '00';
        $seconds = $parts[0][1];
        if ($seconds < 10) {
            $seconds = '0' . $parts[0][1];
        }
        echo $hours . ':' . $minutes . ':' . $seconds;
    } else if ($h) {
        $hours = $parts[0][0];
        $minutes = '00';
        $seconds = '00';
        echo $hours . ':' . $minutes . ':' . $seconds;
    } else if ($m) {
        $minutes = $parts[0][0];
        $seconds = '00';
        echo $minutes . ':' . $seconds;
    } else if ($s) {
        $minutes = '0';
        $seconds = $parts[0][0];
        if ($seconds < 10) {
            $seconds = '0' . $parts[0][0];
        }
        echo $minutes . ':' . $seconds;
    }
}
echo covtime(PT1H2M3S);
0
echo pt2sec("PT3M52S");

function pt2sec($str) {
      preg_match("@PT((?P<hour>[0-9]+)H)?((?P<min>[0-9]+)M)?((?P<sec>[0-9]+)S)?@", $str, $m);
      return (isset($m["hour"])?$m["hour"]*3600:0)+(isset($m["min"])?$m["min"]*60:0)+(isset($m["sec"])?$m["sec"]:0);
}
InvDev
  • 13
  • 3