PHP - Convert ISO8601 standard (used by Youtube) to seconds

Asked

Viewed 209 times

1

I need to convert for seconds a time received in the pattern ISO8601, used by Youtube. Someone knows some function for this purpose?

Exemplifying:

If caught by the Youtube API the length of a video PT1M31S, I’d like to convert all this just to seconds. There is a native PHP function for this? In this example the result would be 91 seconds.

  • 6

    The date for many things, able to help in this case. Now, the way the question is, it forces people to research what ISO8601 is, and know what type of variable and format you’re getting the information from, and even risk giving an answer that’s not what you expect. If you [Dit] the question and put more details, it helps everyone.

  • 1

    I’m not voting as a duplicate, but it’s almost: Compare Youtube and Facebook dates, but I voted on the quality of the question for the reasons set out by Bacco; please improve the presentation of your problem.

  • Friend I know you will understand this as a constructive criticism, do not post answers within the question, already have enough points and have a good time home to know that here we are a Q&A, so the correct is to formulate an answer, if the question is blocked, edit only the part of doubt and it will enter the analysis queue so that other users can vote to reopen, after reopened post the answer. : ) I hope you understand how a good criticism.

  • 1

    @Guilhermenascimento I was waiting to leave the pending state to insert as the right answer. Just what was removed.

2 answers

5

PT1M31S is a time format, meaning time period of 1 minute and 31 seconds, so treat this string as a Datetime. Create a new date and add this range through the method add(), which receives as an argument Dateinterval which is exactly the string PT1M31S, done this just convert the 01:31 in seconds using date_parsed()

$duracao = new DateTime('@0');
$duracao->add(new DateInterval('PT1M31S'));

$parsed = date_parse($duracao->format('H:i:s'));
$segundos = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];

echo $segundos;

Or simply, create a Dateinterval object to translate the time period into hours/minutes/seconds and then perform the calculation to convert these values into seconds. If you use this code in several places I suggest you change the values 3600 and 60 by constants to give greater semantics, eg: const SEGUNDOS_EM_UMA_HORA = 3600

$intervalo = new DateInterval('PT1M31S');
$segundos = $intervalo->h * 3600 + $intervalo->i * 60 + $intervalo->s; 
echo $segundos;

Based on:

Convert youtube Api v3 video Duration in php

How to Convert a "HH:MM:SS" string to Seconds with PHP?

  • Just out of curiosity, what’s this one for '@0' in Datetime?

  • 1

    @Diegofelipe server to specify a Unix timestamp, in case duration will start with 1970-01-01 00:00:00.000000 and not the local date/time. After the call from add() will change to: 1970-01-01 00:01:31.000000

3


Here is an example of a PHP function to convert the Youtube duration format to seconds:

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

   if (count($parts[0]) == 1) {
      array_unshift($parts[0], "0", "0");
   } elseif (count($parts[0]) == 2) {
      array_unshift($parts[0], "0");
   }

   $sec_init = $parts[0][2];
   $seconds = $sec_init%60;
   $seconds_overflow = floor($sec_init/60);

   $min_init = $parts[0][1] + $seconds_overflow;
   $minutes = ($min_init)%60;
   $minutes_overflow = floor(($min_init)/60);

   $hours = $parts[0][0] + $minutes_overflow;

   $conv = $hours.':'.$minutes.':'.$seconds;

   sscanf($conv, "%d:%d:%d", $hours, $minutes, $seconds);
   $time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;

   return $time_seconds;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.