PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Sunrise and Sunset

Zend_Date has also functions integrated for getting informations from the sun. Often it is necessary to get the time for sunrise or sunset within a particularly day. This is quite easy with Zend_Date as just the expected day has to be given and additionally location for which the sunrise or sunset has to be calculated.

As most people do not know the location of their city we have also spent a helper class which provides the location data for about 250 capital and other big cities around the whole world. Most people could use cities near themself as the difference for locations situated to each other can only be measured within some seconds.

For creating a listbox and choosing a special city the function Zend_Date_Cities::getCityList() can be used. It returns the names of all available predefined cities for the helper class.

Example 181. Getting all Available Cities

<?php
// Output the complete list of available cities
print_r (Zend_Date_Cities::getCityList());

The location itself can be received with the Zend_Date_Cities::city() function. It accepts the name of the city as returned by the Zend_Date_Cities::getCityList() function and optional as second parameter the horizon to set.

There are 4 defined horizons which can be used with locations to receive the exact time of sunset and sunrise. The '$horizon' parameter is always optional in all functions. If it is not set, the 'effective' horizon is used.

Table 63. Types of Supported Horizons for Sunset and Sunrise

Horizon Description Usage
effective Standard horizon Expects the world to be a ball. This horizon is always used if non is defined.
civil Common horizon Often used in common medias like TV or radio
nautic Nautic horizon Often used in sea navigation
astronomic Astronomic horizon Often used for calculation with stars

Of course also a self-defined location can be given and calculated with. Therefor a 'latitude' and a 'longitude' has to be given and optional the 'horizon'.

Example 182. Getting the Location for a City

<?php
// Get the location for a defined city
// uses the effective horizon as no horizon is defined
print_r (Zend_Date_Cities::city('Vienna'));

// use the nautic horizon
print_r (Zend_Date_Cities::city('Vienna''nautic'));

// self definition of a location
$mylocation = array('latitude' => 41.5'longitude' => 13.2446);

As now all needed data can be set the next is to create a Zend_Date object with the day where sunset or sunrise should be calculated. For the calculation there are 3 functions available. It is possible to calculate sunset with 'getSunset()', sunrise with 'getSunrise()' and all available informations related to the sun with 'getSunInfo()'. After the calculation the Zend_Date object will be returned with the calculated time.

Example 183. Calculating Sun Information

<?php
// Get the location for a defined city
$city Zend_Date_Cities::city('Vienna');

// create a date object for the day for which the sun has to be calculated
$date = new Zend_Date('10.03.2007'Zend_Date::ISO_8601'de');

// calculate sunset
$sunset $date->getSunset($city);
print 
$sunset->get(Zend_Date::ISO_8601);

// calculate all sun informations
$info $date->getSunInfo($city);
foreach (
$info as $sun) {
    print 
"\n" $sun->get(Zend_Date::ISO_8601);
}

Zend Framework