PHP Sun Information TimeZone Tutorial

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.

This entry was posted in eLearning, 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>