techinc.nl/space/spacestate.php

71 lines
1.6 KiB
PHP

<?php
require_once("language/language.php");
class SpaceState {
private static $obj;
public $open = false;
public $lastchange;
private final function __construct() {
$this->requestJson();
}
public static function get() {
if(!isset(self::$obj)) {
self::$obj = new SpaceState();
}
return self::$obj;
}
public function formattedLastChange() {
global $lang;
$now = time();
$diff = $now - $this->lastchange;
// One second ago
if ($diff == 1) {
return $diff." ".$lang->get("SECOND");
// Less than a minute ago
} else if ($diff < 60) {
return $diff." ".$lang->get("SECONDS");
// One minute ago
} else if ($diff < 120) {
return floor($diff / 60)." ".$lang->get("MINUTE");
// Less than an hour ago
} else if ($diff < 3600) {
return floor($diff / 60)." ".$lang->get("MINUTES");
// One hour ago
} else if ($diff < 7200) {
return floor($diff / 3600)." ".$lang->get("HOUR");
// More than one hour ago
} else {
return floor($diff / 3600)." ".$lang->get("HOURS");
}
}
private function requestJson() {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://techinc.nl/space/spacestate.json",
CURLOPT_TIMEOUT => 1,
CURLOPT_FORBID_REUSE => true,
CURLOPT_RETURNTRANSFER => true
));
$request = curl_exec($ch);
if ($request === false) {
return;
}
$json = json_decode($request);
if (is_null($json)) {
return;
}
$this->open = $json?->open ?? false;
$this->lastchange = $json?->lastchange ?? time();
}
}
$spacestate = SpaceState::get();
?>