Viewing your YouTube subscribed last upload in PHP

I knocked together some PHP code using the YouTube APIĀ  to simply fetch my YouTube subscribed to channels and list the hours/days/weeks/months since last upload.

You can find the code on Gist or in the spoiler at the bottom of this post.

I came up with this as a little challenge to simply list the channels i was subscribed to and the time value since they last uploaded a video, here is a small portion of the output:

youtube sub last uploadAs per the PHP code it is formatted nicely even with a link to the channel. All up this mini project took an hour mostly thanks to having YouTube API code lying around from previous workings.

If you want to use this code yourself all you need is your YouTube channel id and a YouTube api key. Add these into the code at lines 4 and 5.

Here is the php code

See the PHP code
<?php
ini_set('max_execution_time', 120);//2 minutes
error_reporting(0);
$api_key = '';
$your_ch_id = '';
function call_api($call, $type)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $call);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $response = curl_exec($ch);
    curl_close($ch);
    if ($type == 0) {
        return json_decode($response);
    } elseif ($type == 1) {
        return json_decode($response, true);
    }
}

function time_elapsed_string($datetime, $full = false)
{
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);
    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;
    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute'
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }
    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

function yt_recent($ch_id)
{
    $data = call_api("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=" . $ch_id . "&maxResults=1&order=date&type=video&key=" . $api_key . "", 1);
    $videoid = $data['items'][0]['id']['videoId'];//returns video id
    $data2 = call_api("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" . $videoid . "&key=" . $api_key . "", 1);
    $date = $data2['items'][0]['snippet']['publishedAt'];
    $fixed = date('Y-m-d', strtotime($date));
    return time_elapsed_string($fixed);
}

$data = call_api("https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&maxResults=50&channelId=" . $your_ch_id . "&key=" . $api_key . "", 1);

foreach ($data['items'] as $record) {
    $ch_id = $record['snippet']['resourceId']['channelId'];
    $name = $record['snippet']['title'];
    $link = "https://youtube.com/channel/" . $ch_id . "";
    echo "<a href='" . $link . "'>$name </a>: " . yt_recent($ch_id) . "<br><br>";
}