Skip to content

Instantly share code, notes, and snippets.

@JanPetterMG
Last active November 1, 2015 03:22
Show Gist options
  • Save JanPetterMG/1e2a9078c79010fad49f to your computer and use it in GitHub Desktop.
Save JanPetterMG/1e2a9078c79010fad49f to your computer and use it in GitHub Desktop.
Astro widget script, customized for Stian on Diskusjon.no - Forum thread: http://www.diskusjon.no/index.php?showtopic=1682430
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
date_default_timezone_set('Europe/Oslo');
setlocale(LC_TIME, 'nb_NO.UTF-8');
function astro_api_sunrise($parameters)
{
$debug = false;
$api = 'http://api.met.no/weatherapi/sunrise/1.0/?';
foreach ($parameters as $key => $value)
$api .= $key . '=' . $value . ';';
$api = substr($api, 0, -1);
if ($debug) echo $api . "<br>";
$astrodata = simplexml_load_file($api);
$sunrise = array();
foreach ($astrodata->time as $time) {
$current = (string)$time['date'];
// Sunrise and sunset
$sunrise[$current]['sun']['never_rise'] = ($time->location->sun['never_rise'] == 'true') ? true : false;
$sunrise[$current]['sun']['rise'] = (isset($time->location->sun['rise']) ? date('H:i', strtotime($time->location->sun['rise'])) : null);
$sunrise[$current]['sun']['never_set'] = ($time->location->sun['never_set'] == 'true') ? true : false;
$sunrise[$current]['sun']['set'] = (isset($time->location->sun['set']) ? date('H:i', strtotime($time->location->sun['set'])) : null);
// Moonrise and moonset
$sunrise[$current]['moon']['never_rise'] = ($time->location->moon['never_rise'] == 'true') ? true : false;
$sunrise[$current]['moon']['rise'] = (isset($time->location->moon['rise']) ? date('H:i', strtotime($time->location->moon['rise'])) : null);
$sunrise[$current]['moon']['never_set'] = ($time->location->moon['never_set'] == 'true') ? true : false;
$sunrise[$current]['moon']['set'] = (isset($time->location->moon['set']) ? date('H:i', strtotime($time->location->moon['set'])) : null);
// Moon phase
$sunrise[$current]['moon']['phase'] = $time->location->moon['phase'];
switch ($sunrise[$current]['moon']['phase']) {
case 'New moon':
$sunrise[$current]['moon']['phase_no'] = 'nym&aring;ne';
break;
case 'Waxing crescent':
$sunrise[$current]['moon']['phase_no'] = 'voksende nym&aring;ne';
break;
case 'First quarter':
$sunrise[$current]['moon']['phase_no'] = '1/4 m&aring;ne';
break;
case 'Waxing gibbous':
$sunrise[$current]['moon']['phase_no'] = 'voksende halvm&aring;ne';
break;
case 'Full moon':
$sunrise[$current]['moon']['phase_no'] = 'fullm&aring;ne';
break;
case 'Waning gibbous':
$sunrise[$current]['moon']['phase_no'] = 'avtagende fullm&aring;ne';
break;
case 'Third quarter':
$sunrise[$current]['moon']['phase_no'] = '3/4 m&aring;ne';
break;
case 'Waning crescent':
$sunrise[$current]['moon']['phase_no'] = 'avtagende halvm&aring;ne';
break;
default:
$sunrise[$current]['moon']['phase_no'] = $sunrise[$current]['moon']['phase'];
}
}
return $sunrise;
}
function astro_widget_large($address, $days = 7, $show_location = true, $show_phase = true, $lat = null, $lon = null)
{
$location = $address;
// Geocode API parameters
$geocode_param['address'] = $address;
//$geocode_param['key'] = 'INSERT YOUR GOOGLE API_KEY HERE';
// Sunrise API parameters
$sunrise_param['from'] = date('Y-m-d', strtotime('now'));
$sunrise_param['to'] = date('Y-m-d', time() + ($days * 86400) - 86400);
//$sunrise_param['date'] = date('Y-m-d', strtotime('now'));
$sunrise_param['lat'] = $lat;
$sunrise_param['lon'] = $lon;
// Google Maps: Geocode API request
if (isset($geocode_param['address']) && !isset($sunrise_param['lat']) && !isset($sunrise_param['lon'])) {
$geocode = astro_api_geocode($geocode_param);
$sunrise_param['lat'] = $geocode['lat'];
$sunrise_param['lon'] = $geocode['lon'];
$location = $geocode['name'];
}
// Meteorologisk institutt: Sunrise API request
$sunrise = astro_api_sunrise($sunrise_param);
// Set location text
$location_text = (isset($location) && $show_location) ? ' for ' . $location : '';
?>
<div class="astrowidget astrowidgetL">
<!-- Astro widget by jpg: http://jpg.priv.no/scripts/astro/ -->
<table>
<tr>
<th colspan="<?php echo ($show_phase) ? 6 : 5; ?>">
<p style="font-size: 150%;">Sol og m&aring;ne<?php echo $location_text; ?></p>
</th>
</tr>
<tr>
<th>
<p><img src="http://jpg.priv.no/pic/astro/solarsystem50h.png"
style="max-height: 50px; max-width: 128px;"/>
</p>
</th>
<th colspan="2">
<p style="white-space: nowrap;">opp<img src="http://jpg.priv.no/pic/astro/sun50h.png"
style="max-height: 50px;"/>ned</p>
</th>
<th colspan="2">
<p style="white-space: nowrap;">opp<img src="http://jpg.priv.no/pic/astro/moon50h.png"
style="max-height: 50px;"/>ned</p>
</th>
<?php
if ($show_phase) {
?>
<th>
<p><img src="http://jpg.priv.no/pic/astro/moonphase128w.png"
style="max-width: 128px; max-height: 50px;"/></p>
</th>
<?php
}
?>
</tr>
<?php
foreach ($sunrise as $key => $value) {
?>
<tr>
<td>
<p style="font-size: 110%; text-align: center; text-wrap: avoid;"><?php echo ucfirst(strftime('%A %e. %b', strtotime($key))); ?></p>
</td>
<?php
if ($value['sun']['never_rise']) {
?>
<td colspan="2" style="text-align: center;">
<p>nede hele dagen</p>
</td><?php
} elseif ($value['sun']['never_set'] && !isset($value['sun']['rise'])) {
?>
<td colspan="2" style="text-align: center;">
<p>midnattssol</p>
</td><?php
} else {
?>
<td>
<p><?php echo (isset($value['sun']['rise'])) ? $value['sun']['rise'] : '&nbsp;'; ?></p>
</td>
<td>
<p><?php echo (isset($value['sun']['set'])) ? $value['sun']['set'] : '&nbsp;'; ?></p>
</td>
<?php
}
if ($value['moon']['never_rise']) {
?>
<td colspan="2" style="text-align: center;">
<p>ingen m&aring;ne</p>
</td><?php
} elseif ($value['moon']['never_set'] && !isset($value['moon']['rise'])) {
?>
<td colspan="2" style="text-align: center;">
<p>oppe hele tiden</p>
</td><?php
} else {
?>
<td>
<p><?php echo (isset($value['moon']['rise'])) ? $value['moon']['rise'] : '&nbsp;'; ?></p>
</td>
<td>
<p><?php echo (isset($value['moon']['set'])) ? $value['moon']['set'] : '&nbsp;'; ?></p>
</td>
<?php
}
if ($show_phase) {
$previous_phase = (isset($previous_phase)) ? $previous_phase : null;
$current_phase = (isset($current_phase)) ? $current_phase : null;
foreach ($sunrise as $key2 => $value2) {
$some_phase = (isset($value2['moon']['phase_no']) && !$value2['moon']['never_rise']) ? $value2['moon']['phase_no'] : '';
if ($key === $key2) {
$current_phase = (isset($value['moon']['phase_no']) && !$value['moon']['never_rise']) ? $value['moon']['phase_no'] : '';
if ($current_phase != $previous_phase) {
$rowspan_phase = 0;
}
}
if (isset($rowspan_phase) && $current_phase == $some_phase) {
$rowspan_phase++;
} elseif (isset($rowspan_phase) && $current_phase != $some_phase) {
break;
}
}
if (isset($rowspan_phase)) {
$previous_phase = $current_phase;
?>
<td rowspan="<?php echo $rowspan_phase; ?>" style="text-align: center;">
<p><?php echo (isset($value['moon']['phase_no']) && !$value['moon']['never_rise']) ? ucfirst($value['moon']['phase_no']) : '&nbsp;'; ?></p>
</td>
<?php
unset($rowspan_phase);
}
}
?>
</tr>
<?php } ?>
<tr>
<th colspan="<?php echo ($show_phase) ? 6 : 5; ?>">
<p style="color: gray; font-size: 75%; font-weight: normal;">
Astrodata levert av <a href="http://met.no/" style="color: gray; text-decoration: none;"
target="_blank">met.no</a>
</p>
</th>
</tr>
</table>
</div>
<?php
}
?>
<style>
.astrowidget table {
border-collapse: collapse;
}
.astrowidget p {
margin: 5px 10px 5px 10px;
}
.astrowidgetL td {
border: 1px solid lightgray;
}
</style>
<?php astro_widget_large('Skei Fjellandsby', 7, true, true, '61.3346628', '10.0874576'); ?>
<br>
<?php astro_widget_large('Oslo', 7, true, true, '59.8096749', '10.6225688'); ?>
<br>
<?php astro_widget_large('Svalbard', 7, true, true, '76.4362793', '10.4612168'); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment