Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert youtube Api v3 video duration in php

Tags:

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;
        }
like image 231
Daniel Euchar Avatar asked Jun 24 '14 17:06

Daniel Euchar


People also ask

How to use YouTube Data API v3?

Under the YouTube APIs section, click the YouTube Data API link. Enable the YouTube Data API v3 to access the YouTube data by clicking the uppercased ENABLE button. Click the Credentials link on the left navigation menu. Press the “Create credentials button” selecting the API key. A dialog box will appear with your newly created API Key.

How to convert duration of a video to time in Python?

For Python, a great library to convert the duration is aniso8601. It returns a datetime.timedelta, which can be formatted to basically anything. What videos are weeks long on YouTube?!? I got it!

How to get data from a YouTube video using HTML form?

HTML form requests a YouTube video URL for the given input. When the user pastes the URL into the input field and submits it, this URL variable will be sent to the PHP file. After getting data from the URL, it will be previewed on the browser. YouTube Data API is available free for usage.

Is there a PHP library for YouTube API?

Google APIs Client Library for PHP on GitHub and autogenerated classes for the YouTube API. (On the page, find the YouTube folder and the YouTube.php file. Lets your application retrieve YouTube content while also enabling YouTube users to manage their YouTube accounts.


1 Answers

Using DateTime class

You could use PHP's DateTime class to achieve this. This solution is much simpler, and will work even if the format of the quantities in the string are in different order. A regex solution will (most likely) break in such cases.

In PHP, PT2H34M25S is a valid date period string, and is understood by the DateTime parser. We then make use of this fact, and just add it to the Unix epoch using add() method. Then you can format the resulting date to obtain the required results:

function covtime($youtube_time){
    if($youtube_time) {
        $start = new DateTime('@0'); // Unix epoch
        $start->add(new DateInterval($youtube_time));
        $youtube_time = $start->format('H:i:s');
    }
    
    return $youtube_time;
}   

echo covtime('PT2H34M25S');

Demo

like image 160
Amal Murali Avatar answered Sep 18 '22 23:09

Amal Murali