The sunlight pie chart I was using relies on deprecated Google API's, so I was looking for an alternative display. Over in the Cumulus forum Mark Crossley and others have created a nice 24 hr. clock-like display that serves admirably. The script to create the pie chart is called '
heavens.php', a copy of which you can download and save. I've modified it to save and cache a PNG graphic rather than displaying the pie chart 'in place'. This pie chart can then be displayed
in the menubar and/or the dashboard. I've copied a legend from a graphic on the Cumulus board and use a mouseover link to display it in my menubar, but I don't have room for it in my dashboard. The defaults in 'heavens.php' should work for most purposes. The cache timeout is set at 2 minutes because small changes in the location of the red 'hour-hand' are perceptible.
The code to display the pie chart itself is fairly straightforward, but displaying the difference in daylight from yesterday to today is a little less so. This code snippet displays both the pie chart image with the legend and the day difference:
<p class="sideBarTitle"><?php langtrans('Sunlight Hours'); ?></p>
<div class="sideBarText" style="font-weight:lighter; font-size: 6.5pt; text-align: center; margin-bottom:4px; line-height:135%">
<?php
include("heavens.php");
$hrsOfSunLight = array(); // [0] = yesterday, [1] = today, [2] = tomorrow
$changeinday2 = get_changeinday();
# echo '$changeinday2 = '.$changeinday2.'<br/>'; // Debug Code
//Make piechart
$timebits = explode(":", $hrsOfSunLight[1]); // Today
$testresult = $timebits[0] + $timebits[1]/60;
$test1jus=round(($testresult/24)*100,1);
$testmorker=100-$test1jus;
?>
<span class="thisPage" style="position: relative; font-weight:lighter">
<?php
$time_arr = explode(':', $changeinday2);
$time_arr[0]>0?$cidsign = '+': $cidsign ='-';
if ($time_arr[1]< 5) {
if ($displayYesterday) {
$time_arr2 = explode(':', $hrsOfSunLight[0]); // Yesterday
echo (int)$time_arr2[0] . " hrs " . (int)$time_arr2[1] . " min " . (int)$time_arr2[2]." sec";
echo '<br/>Sunlight Yesterday<br/>';
}
$time_arr = explode(':', $hrsOfSunLight[1]); // Today
echo (int)$time_arr[0] . " hrs " . (int)$time_arr[1] . " min " . (int)$time_arr[2]." sec";
echo '<br/>of Sunlight Today';
?>
<?php echo '<br/>Which is ';
$time_arr = explode(':', $changeinday2);
echo (((int)$time_arr[1]>0)?('<br/>'.(int)$time_arr[1] . ' min '):' ') . $time_arr[2];
echo (substr($time_arr[0],0,1) != "-")?' sec Longer':' sec Shorter'; // '-' when getting shorter
echo ('<br/>Than Yesterday');
if ($displayTomorrow) {
$time_arr3 = explode(':', $hrsOfSunLight[2]); // Tomorrow
echo (int)$time_arr3[0] . " hrs " . (int)$time_arr3[1] . " min " . (int)$time_arr3[2]." sec";
echo '<br/>Sunlight Tomorrow<br/>';
} ?>
</span>
<span class="thisPage" style="font-size:7px"><br/>(Noon)</span>
<a style="width:109px" href="<?php echo $sunLightPieLink?>" target="_blank" title="Astronomy Page in New Tab"><img style="width:109px; margin-left:auto; margin-right:auto" src="<?php echo getUniqueImageURL('./cache/heavenpie.png')?>" alt="graph"/></a>
<span class="thisPage" style="font-size:7px">(Midnight)<br/>
</span>
<span class="thisPage" style=" position:relative; font-weight:lighter">
<a style="cursor:pointer; text-align:center; width:109px" onmouseover="show_legend('legend')" onmouseout="show_legend()">Legend</a><img id="legend" style="display:none" src="./ajax-images/heavensLegend.png" alt="Legend"/>
</span>
<?php } else { echo 'Unavailable<br/> '; ?></span><?php }
?>
</div>
Note that two dependent functions are used in this snippet, 'get_changeinday()' and 'getUniqueImageURL()'. I have added both of these functions to my 'common.php'. Here is the code:
// -----------------------------------------------------------------------------
// CHANGE IN DAYLENGTH FUNCTION: computes length of yesterday, today, & tomorrow
// and then the change in day length from yesterday to today
// Written by SE Lincoln Weather .
// -----------------------------------------------------------------------------
// Alternate $changeinday routine that finds $changeinday2
function get_changeinday() {
global $hrsOfSunLight;
global $SITE;
global $displayTomorrow;
$t = time()-86400; // Yesterday
for ($i=0; $i<(2+($displayTomorrow?1:0)); $i++) { // Only loop twice if no $displayTomorrow
$sun = date_sun_info($t, $SITE['latitude' ], $SITE["longitude"]);
$srise = $sun["sunrise"];
$sset = $sun["sunset"];
$sunhrs[$i] = $sset - $srise; // $sunhrs is time between sunrise and sunset
$sh[$i] = floor($sunhrs[$i]/3600); // Sun Hours
$sm[$i] = floor((($sunhrs[$i]/3600)-$sh[$i])*60); // Sun Minutes
$ss[$i] = round(((((($sunhrs[$i]/3600)-$sh[$i])*60)-$sm[$i])*60),0); // Sun Seconds
$hrsOfSunLight[$i] = $sh[$i].':'.$sm[$i].':'.$ss[$i]; // Assemble strings in array
$t+=86400; // Next Day
}
$cindsign = (($sunhrs[1]-$sunhrs[0])>0)?'+':'-'; // Change IN Day sign
$shdiff = ($cindsign=='-'?($sh[0] - $sh[1]):($sh[1] - $sh[0])); // Difference in hours
$smdiff = ($cindsign=='-'?($sm[0] - $sm[1]):($sm[1] - $sm[0])); // Difference in minutes
$ssdiff = ($cindsign=='-'?($ss[0] - $ss[1]):($ss[1] - $ss[0])); // Difference in seconds
if ($ssdiff<0) { // negative number of seconds?
$smdiff -= 1; // borrow
$ssdiff += 60; // and carry
}
if ($smdiff<0) { // negative number of minutes?
$shdiff -=1; // borrow
$smdiff += 60; // and carry
}
$changeinday2 = $cindsign.$shdiff.':'.$smdiff.':'.$ssdiff; // Assemble cind string
return $changeinday2;
}
// end of changeinday()
and
if (!function_exists('getUniqueImageURL')) {
function getUniqueImageURL($image_url){
$timestamp = time();
if(strpos($image_url, '?')){
$image_url = str_replace('?', "?$timestamp&", $image_url);
}
else{
$image_url .= "?$timestamp";
}
return $image_url;
}
}
This latter function prevents browser image caching of the 'heavenpie.png' image. I use it liberally on my website so I need to check to see if it has already been declared whenever I want to use it. Declaring it twice WILL produce a fatal error.
The Legend is available
here (right click and 'Save as...').
I have anchored a link to my astronomy page, '$sunLightPieLink', on the sunlight pie chart. The variable is in my 'Settings.php' as:
$sunLightPieLink = 'wxastronomy_new.php'; // Link to Astronomy Page
Be sure to check this link and point it at your astronomy page. You can hardcode this link by replacing '$sunLightPieLink' with the relative URL of your astronomy page, but I would recommend using a 'Settings.php' entry for ease of maintenance.