PHP Sun Information Software Integration Tutorial

PHP Sun Information Software Integration Tutorial

PHP Sun Information Software Integration Tutorial

We left off the project of PHP Sun Information TimeZone Tutorial recently adding in that fourth (time) dimension link via the use of TimeZones, and today, that …

  • Sun Information is integrated into …
  • Sun Angle project

… and vice versa making a “win win” scenario, we hope, for both.

The PHP date/time sun information method we use does not calculate a sun elevation (Α) at “noon” exactly, but we are going to settle for this as a sun elevation at the transit point, and if you read (the very helpful) link (thanks) …

Α = 90 + Θ – Δ

When the equation above gives a number greater than 90° then substract the result from 180°. It means the sun at solar noon is coming from the south as is typical the northern hemisphere.

where:
Θ is the latitude of the location of interest (+ve for the northern hemisphere and -ve for the southern hemisphere).
Δ is the declination angle, which depends on the day of the year.

… you may be happy with all but that “sun declination” idea above, if you are anything like me?! You’ll read that almanacs can be used to calculate these, but we got to see this great link‘s …

Determine how many days have passed since January 1st. For example, the number of days between January 1st and February 14th is 44.

Add ten to the number of days passed. Write this number down. Following the example, adding 10 to 44 gives 54.

Divide 360 by the number of days in the year. Every year has 365 days except leap years. Write this number down. From the example, 360 divided by 365 = 0.9863.

Multiply the number from Step 2 (the approximate number of days that have passed since the winter solstice) by the amount from Step 3 (the degree of rotation per day). Write down the result. From the example, 54 times .9863 equals 53.2603.

Find the cosine of the result from Step 4. Multiply it by -23.44, the tilt of the Earth’s axis in degrees. The result is the solar declination in degrees for that day of the year. From the example, the cosine of 53.2603 is 0.5982; multiply it by -23.44 to get -14.02 degrees.

… advice, to be able to piece together a quite acceptable online method of calculating these “sun declinations”.

That was that, the mathematics of it, where “the net” is so good at offering advice, but “inhouse integration” of web applications, that is another matter entirely, and as we’ve noted before, often, integration of existant arrangements can be harder work that starting from scratch … bit like renovating (versus building from vacant land), quite often?!

Today’s integration is a bit rare, in that between two pieces of functionality, the integrations are two-way … so that

We hope you try out some of this PHP work.


Previous relevant PHP Sun Information TimeZone Tutorial is shown below.

PHP Sun Information TimeZone Tutorial

PHP Sun Information TimeZone Tutorial

What links time and place? … Give up?

What links time and place when you are programming, a lot? … Give up?

What links time and place when you are PHP programming, heaps? … Give up?

TimeZones … and PHP is soooooooo good at it in terms of its DateTimeZone class. So it seems logical that to involve TimeZones in (amongst the logic of) our recent PHP Sun Information Primer Tutorial could help it. My oath it can! Don’t worry, I’ve told off the fourth finger of my left hand to stop using the word ‘can’.

Its immediately apparent “weakness” is that it presents the time of sunset and sunrise in Greenwich Mean Time (GMT), back then the way to go, because we hadn’t thought to use TimeZones in any meaningful way. To use TimeZones in a meaningful way could mean (at least) one of two things …

  • the timezone at the web server (where the URL you got to “surfing the net” bases its time on) … or … spoiler alert … today’s much more relevant …
  • client timezone being the timezone of the place where the user is located with their computing device

To try to help the user out here, then, you can’t use anything PHP, which is a serverside language, to determine, programmatically, what is a (good guess regarding what is a) user’s default timezone. In times past we don’t think we’ve stumbled upon the Javascript …


var tzis='' + Intl.DateTimeFormat().resolvedOptions().timeZone; // Thanks to https://stackoverflow.com/questions/22618056/javascript-timezone-information-how-to-get-america-los-angeles-or-equivalent/22625076#22625076
if (tzis != '') document.getElementById('timezone').value=tzis;

… that we now add up the top of this PHP program’s onload ol() Javascript function, so that the serverside passes the batton onto the clientside, for help setting a (probably more apt than GMT) default timezone to use as the basis for showing sunrise and setset (and other sun information) times relevant to a particular geographical location. The HTML form …

  • method=POST
  • action=./sun_information.php

… completes the circle of all sides of the (client/server) equation knowing what is a good timezone on which to base your displayed time information.

The live run link now uses this PHP sun_information.php source code that changed in this way to facilitate that improvement.


Previous relevant PHP Sun Information Primer Tutorial is shown below.

PHP Sun Information Primer Tutorial

PHP Sun Information Primer Tutorial

PHP has some great code features built into versions as early as PHP 4, even earlier perhaps, though today’s work needs PHP 5. Today, we’ve built a pretty simple web application around PHP’s date/time sun information function …

date_sun_info

(PHP 5 >= 5.1.2, PHP 7)

date_sun_info β€” Returns an array with information about sunset/sunrise and twilight begin/end

It’s here at this live run link that uses the PHP sun_information.php source code you can download, and try yourself, if you like. Within this code we allow, if access permits the user’s current location be reflected in default latitude and longitude HTML form values via Javascript navigator.geolocation.getCurrentPosition(success[, error[, options]]) call.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Event-Driven Programming, Tutorials and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>