Skip to content

Instantly share code, notes, and snippets.

@cp6
Created July 9, 2018 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cp6/8dcdac191c59de611677f4db80556e8b to your computer and use it in GitHub Desktop.
Save cp6/8dcdac191c59de611677f4db80556e8b to your computer and use it in GitHub Desktop.
View when your YouTube subscribed last upload was
<?php
ini_set('max_execution_time', 120);//2 minutes
error_reporting(0);
$api_key = '';//Your YouTube/Google api key
$your_ch_id = '';//YOur YouTube channel 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>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment