News Archive
PhpRiot Newsletter
Your Email Address:

More information

Weather from the Norwegians, sunset and sunrise

Note: This article was originally published at Planet PHP on 21 December 2012.
Planet PHP

Weather from the Norwegians, sunset and sunrise

London, UK Friday, December 21st 2012, 11:12 GMT

I have recently acquired a Raspberry PI and as a hacking project I am using it as status screen in the living room. As part of this, it has a weather widget.

I use the weather data from ("the Norwegians") as they provide free weather data for the whole world. For each location, there is a nice XML file with the weather forecast.

The XML file has a forecast for several periods. See for example the first upcoming one:


This shows among others, the period (December 21st, 12:00 to 13:00), the symbol to use (number="1"), the expected precipitation and temperature. There is a description of what the symbols mean. The page is in Norwegian, but that's all right, as I can actually read that.

For most of the symbols there is both a day and a night variant of the symbol. And in some cases, there is also a "mArketid" variant - the period in which the sun never comes over the horizon.

In each XML file, the coordinates of the location for which the forecast is are also present:

LondonCapitalUnited Kingdom

The location information comes from, which handily also includes the Olson timezone ID for the location. The timezone ID combined with the time, the latitude and the longitude are enough to calculate sunrise and sunset times with PHP's date_sun_info() function.

For example, to find out the sunset and sunrise for today in London (Europe/London, 51.50833AN, 0.12574AW):

This returns the following array:

Array ([sunrise] = 1356077046 [sunset] = 1356105238 [transit] = 1356091142 [civil_twilight_begin] = 1356074637 [civil_twilight_end] = 1356107647 [nautical_twilight_begin] = 1356072046 [nautical_twilight_end] = 1356110239 [astronomical_twilight_begin] = 1356069597 [astronomical_twilight_end] = 1356112687)

Each value is a Unix timestamp. To display when the Sun rises and sets:

Which shows (for today): Sunrise: 08:04, Sunset: 15:53. The transit value is the time when the Sun is at its highest point, and the different twilight_begin/twilight_end values represent different start and end times of the three different forms of twilight. From the above information, it is also trivial to calculate the length of day: echo $result['sunset'] - $result['sunrise']); (or today in London: 28192 seconds, which is 7h49m52s).

Near the summer and winter solstices there are regions on Earth where

Truncated by Planet PHP, read more at the original (another 4130 bytes)