Last active
February 3, 2025 22:11
-
-
Save macsplit/2ee96888aa11fbf5a73970ee4010e88b to your computer and use it in GitHub Desktop.
Get Weather from 7Timer!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('UTC'); | |
$lon = "-0.19759370325694858"; | |
$lat = "51.35956063540873"; | |
$url = "https://www.7timer.info/bin/civil.php?lon=$lon&lat=$lat&unit=metric&ac=0&output=json"; | |
$weathers = [ | |
'tsday' => "☀️ Hot Day", | |
'clearday' => "☀️ Clear Day", | |
'clearnight' => "🌙 Clear Night", | |
'pcloudyday' => "⛅ Partially Cloudy Day", | |
'pcloudynight' => "☁️ Partially Cloudy Night", | |
'mcloudyday' => "🌥️ Moderately Cloudy Day", | |
'mcloudynight' => "☁️ Moderately Cloudy Night", | |
'cloudyday' => "☁️ Cloudy Day", | |
'cloudynight' => "☁️ Cloudy Night", | |
'humidday' => "💦 Muggy Day", | |
'humidnight' => "💦 Muggy Night", | |
'lightrainday' => "🌧️ Lightly Rainy Day", | |
'lightrainnight' => "🌧️ Lightly Rainy Night", | |
'oshowerday' => "🌧️ Overcast Showery Day", | |
'oshowernight' => "🌧️ Overcast Showery Night", | |
'ishowerday' => "🌧️ Showery Day", | |
'ishowernight' => "🌧️ Showery Night", | |
'lightsnowday' => "❄️ Lightly Snowy Day", | |
'lightsnownight' => "❄️ Lightly Snowy Night", | |
'rainday' => "🌧️ Rainy Day", | |
'rainnight' => "🌧️ Rainy Night", | |
'snowday' => "❄️ Snowy Day", | |
'snownight' => "❄️ Snowy Night", | |
'rainsnowday' => "🌨️ Rainy and Snowy Day", | |
'rainsnownight' => "🌨️ Rainy and Snowy Night" | |
]; | |
$json = file_get_contents($url); | |
if (!$json) exit(); | |
$obj = json_decode($json); | |
$init = DateTime::createFromFormat('YmdH', $obj->init)->getTimestamp(); | |
$series = $obj->dataseries; | |
$now = time(); | |
foreach ($series as $k => $point) { | |
$timepoint = $point->timepoint; | |
$timestamp = $init + ($timepoint * 3600); | |
$diff = abs($timestamp - $now); | |
$data[$diff] = $point; | |
} | |
$key = min(array_keys($data)); | |
$measure = $data[$key]; | |
$temperature = $measure->{'temp2m'}; | |
$weather_desc = $weathers[$measure->weather]; | |
echo "$weather_desc $temperature"."°C\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment