Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Set names to lowercase for URL injection.
- $BroadcasterLower = strtolower($_GET["Broadcaster"]);
- // Set timezone to match Twitch servers for JSON date/ time.
- date_default_timezone_set("Greenwich");
- // Inject lowercase name.
- $ch = curl_init("https://api.twitch.tv/kraken/streams/" . $BroadcasterLower);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $result = curl_exec($ch);
- curl_close($ch);
- // Decode the above JSON data.
- $StreamData = json_decode($result, true);
- $UpTimeDateTime =date('Y-m-d H:i:s', strtotime($StreamData['stream']['created_at']));
- // Stream offline
- if($StreamData['stream'] == null)
- {
- echo 'Sorry, ' . htmlspecialchars($_GET["Broadcaster"]) . ' isn\'t currently streaming, check back later or follow and enable notifications to know when ' . htmlspecialchars($_GET["Broadcaster"]) . ' streams next!';
- die();
- }
- echo $StreamData['stream']['channel']['display_name'] . ' has been streaming ' . $StreamData['stream']['channel']['game'] . ' for the past ';
- echo GetDateDifference($UpTimeDateTime);
- echo '.';
- // Function to get the date difference.
- function GetDateDifference($datetime)
- {
- $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',
- 's' => 'second',
- );
- foreach ($string as $k => &$v) {
- if ($diff->$k) {
- $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
- }
- else
- {
- unset($string[$k]);
- }
- }
- return $string ? implode(', ', $string) . '' : '';
- }
- ?>
- Example: Uptime.php?Broadcaster=TwitchBroadcaster
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement