PHP Function Reference

PHP date_sun_info() Function



The PHP date_sun_info() function returns an array with information about sunset/sunrise and begin/end of the twilight, in the given location.

Syntax

date_sun_info(timestamp, latitude, longitude)

Parameters

timestamp Required. Specify the Unix timestamp.
latitude Optional. Specify the latitude of the location in degrees.
longitude Optional. Specify the longitude of the location in degrees.

Return Value

Returns array on success or false on failure. The structure of the array is detailed in the following list:

  • sunrise : The timestamp of the sunrise (zenith angle = 90°35')
  • sunset : The timestamp of the sunset (zenith angle = 90°35').
  • transit : The timestamp when the sun is at its zenith, i.e. has reached its topmost point.
  • civil_twilight_begin : The start of the civil dawn (zenith angle = 96°). It ends at sunrise.
  • civil_twilight_end : The end of the civil dusk (zenith angle = 96°). It starts at sunset.
  • nautical_twilight_begin : The start of the nautical dawn (zenith angle = 102°). It ends at civil_twilight_begin.
  • nautical_twilight_end : The end of the nautical dusk (zenith angle = 102°). It starts at civil_twilight_end.
  • astronomical_twilight_begin : The start of the astronomical dawn (zenith angle = 108°). It ends at nautical_twilight_begin.
  • astronomical_twilight_end : The end of the astronomical dusk (zenith angle = 108°). It starts at nautical_twilight_end.

The values of the array elements are either UNIX timestamps, false if the sun is below the respective zenith for the whole day, or true if the sun is above the respective zenith for the whole day.

Example: date_sun_info() example

The example below shows the usage of date_sun_info() function.

<?php
//London, UK - Latitude: 51.51 North, Longitude: 0.13 West

//creating timestamp for some summer and winter days
$summer = mktime(0,0,0,06,30,2016);  //30 Jun 2016
$winter = mktime(0,0,0,12,31,2016);  //31 Dec 2016

//getting sun info for $summer
echo("London, UK  Date: ".date("d-M-Y", $summer));
echo "\n".str_repeat("-",29)."\n";
$sun_info = date_sun_info($summer, 51.51,-0.13);
foreach ($sun_info as $key => $val)
  echo "$key: " . date("H:i:s", $val) . "\n";

echo "\n";

//getting sun info for $winter
echo("London, UK  Date: ".date("d-M-Y", $winter));
echo "\n".str_repeat("-",29)."\n";
$sun_info = date_sun_info($winter, 51.51,-0.13);
foreach ($sun_info as $key => $val) 
  echo "$key: " . date("H:i:s", $val) . "\n";
?>

The output of the above code will be:

London, UK  Date: 30-Jun-2016
-----------------------------
sunrise: 03:45:10
sunset: 20:23:24
transit: 12:04:17
civil_twilight_begin: 03:00:11
civil_twilight_end: 21:08:23
nautical_twilight_begin: 01:47:31
nautical_twilight_end: 22:21:03
astronomical_twilight_begin: 00:00:01
astronomical_twilight_end: 00:00:01

London, UK  Date: 31-Dec-2016
-----------------------------
sunrise: 08:04:03
sunset: 16:03:23
transit: 12:03:43
civil_twilight_begin: 07:26:15
civil_twilight_end: 16:41:11
nautical_twilight_begin: 06:43:18
nautical_twilight_end: 17:24:08
astronomical_twilight_begin: 06:02:39
astronomical_twilight_end: 18:04:47

Example: getting info for different places on same day

Consider one more example where the information is collected for different places on the same day.

<?php
//creating timestamp for some day
$day = mktime(0,0,0,06,30,2016);  //30 Jun 2016

//getting sun info for London, UK - 
//Latitude: 51.51 North, Longitude: 0.13 West
echo("London, UK  Date: ".date("d-M-Y", $day));
echo "\n".str_repeat("-",29)."\n";
$sun_info = date_sun_info($day, 51.51, -0.13);
foreach ($sun_info as $key => $val) 
  echo "$key: " . date("H:i:s", $val) . "\n";

//getting sun info for Lisbon, Portugal - 
//Latitude: 38.4 North, Longitude: 9 West
echo("\nLisbon, Portugal  Date: ".date("d-M-Y", $day));
echo "\n".str_repeat("-",35)."\n";
$sun_info = date_sun_info($day, 38.4, -9);
foreach ($sun_info as $key => $val) 
  echo "$key: " . date("H:i:s", $val) . "\n";

//getting sun info for Oslo, Norway - 
//Latitude: 59.91 North, Longitude: 10.75 East
echo("\nOslo, Norway  Date: ".date("d-M-Y", $day));
echo "\n".str_repeat("-",31)."\n";
$sun_info = date_sun_info($day, 59.91, 10.75);
foreach ($sun_info as $key => $val) 
  echo "$key: " . date("H:i:s", $val) . "\n";
?>

The output of the above code will be:

London, UK  Date: 30-Jun-2016
-----------------------------
sunrise: 03:45:10
sunset: 20:23:24
transit: 12:04:17
civil_twilight_begin: 03:00:11
civil_twilight_end: 21:08:23
nautical_twilight_begin: 01:47:31
nautical_twilight_end: 22:21:03
astronomical_twilight_begin: 00:00:01
astronomical_twilight_end: 00:00:01

Lisbon, Portugal  Date: 30-Jun-2016
-----------------------------------
sunrise: 05:14:09
sunset: 20:05:23
transit: 12:39:46
civil_twilight_begin: 04:44:08
civil_twilight_end: 20:35:25
nautical_twilight_begin: 04:04:29
nautical_twilight_end: 21:15:03
astronomical_twilight_begin: 03:19:22
astronomical_twilight_end: 22:00:10

Oslo, Norway  Date: 30-Jun-2016
-------------------------------
sunrise: 01:56:17
sunset: 20:45:14
transit: 11:20:46
civil_twilight_begin: 00:22:42
civil_twilight_end: 22:18:49
nautical_twilight_begin: 00:00:01
nautical_twilight_end: 00:00:01
astronomical_twilight_begin: 00:00:01
astronomical_twilight_end: 00:00:01

❮ PHP Date and Time Reference