how do i convert PT2H34M25S to 2:34:25
I searched and used this code. I'm new to regex can someone explain this ? and help me with my issue
function covtime($youtube_time){
preg_match_all('/(\d+)/',$youtube_time,$parts);
$hours = floor($parts[0][0]/60);
$minutes = $parts[0][0]%60;
$seconds = $parts[0][1];
if($hours != 0)
return $hours.':'.$minutes.':'.$seconds;
else
return $minutes.':'.$seconds;
}
but this code only give me HH:MM
so dumb found the solution :
function covtime($youtube_time){
preg_match_all('/(\d+)/',$youtube_time,$parts);
$hours = $parts[0][0];
$minutes = $parts[0][1];
$seconds = $parts[0][2];
if($seconds != 0)
return $hours.':'.$minutes.':'.$seconds;
else
return $hours.':'.$minutes;
}
DateTime and DateInterval not working in Yii or some php version. So this is my solution. Its work with me.
You should take a look at your
$parts
variable after it's created.Compare that output with how you are defining your variables. It should stand out pretty obviously after that.
I guess my next question after that is what variations of the input time string are you expecting and what validation will you be doing? The variations of the input format you want to handle will affect the complexity of the actual code written.
Edit: Here is an updated function to handle missing numeric values (if the hours or minutes is omitted) and seconds/hours over 60 (not sure if this will ever happen). This doesn't validate the label for the numbers:
More validation can be added to look into validating the input string.
Both Amal's and Pferate's solutions are great! However, Amal solution keep giving me 1Hr extra. Below is my solution which is same with Amal but difference approach and this work for me.
datetime->add()
referenceHere is the complete code to get, convert and display the video duration
Replace xxxxxxx with your API Key Results: 13:07
You can try this -
Why the complication. Think outside the box.
OK. preg_replace is not really needed, but I put it to guarantee only numbers.
Then for my formatting (which you can do however you like without limits),