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?
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.– Bacco
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.
– brasofilo
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.
– Guilherme Nascimento
@Guilhermenascimento I was waiting to leave the pending state to insert as the right answer. Just what was removed.
– Daniel Accorsi