Categories
PHP

Sunset, Sunrise, Transit and Twilight

How to get information about sunset/sunrise, transit and twilight begin/end for a specified date, longitude and latitude.

  1. date_sunrise() (deprecated as of PHP 8.1)
    This function returns the time of sunrise for a specified day and location.
  2. date_sunset() (deprecated as of PHP 8.1)
    This function returns the time of sunset for a specified day and location.
  3. date_sun_info():
    This function returns an array containing information about sunset/sunrise and twilight begin/end for a given date, longitude and latitude.

Determining Sunrise and Sunset

The date_sunrise() and date_sunset() functions have been DEPRECATED as of PHP 8.1.0. Use date_sun_info() instead.

date_sunrise():

<?php
 //Syntax

 date_sunrise(
  int $timestamp,
  int $returnFormat = SUNFUNCS_RET_STRING,
  ?float $latitude = null,
  ?float $longitude = null,
  ?float $zenith = null,
  ?float $utcOffset = null
 ): string|int|float|false

date_sunset():

<?php
 //Syntax

 date_sunrise(
  int $timestamp,
  int $returnFormat = SUNFUNCS_RET_STRING,
  ?float $latitude = null,
  ?float $longitude = null,
  ?float $zenith = null,
  ?float $utcOffset = null
 ): string|int|float|false

These functions return time of sunset/sunrise (specified as a timestamp) for a given day and location. This function accepted the following parameters:

  1. timestamp timestamp of the day
  2. returnFormat
    • SUNFUNCS_RET_STRING returns string
    • SUNFUNCS_RET_DOUBLE returns float
    • SUNFUNCS_RET_TIMESTAMP returns integer (timestamp)
  3. latitude default north
  4. longitude default east
  5. zenith
  6. utcOffset in hours

Depending on the current location and date, the times for sunrise and sunset can drastically vary. However, formulas exist for determining this value depending on latitude and longitude, and PHP has this functionality integrated into its core starting with PHP 5. All you are required to do is call date_sunrise() and date_sunset(). Both functions expect a number of parameters:

  • A timestamp (epoche value) of the date for which to determine the sunrise/sunset
  • The desired format for the return value:
    SUFUNCS_RET_DOUBLE returns the time as a float value (between 0 and 23.99),
    SUNFUNCS_RET_STRING returns it as a string (between 00:00 and 23:59), and
    SUNFUNCS_RET_TIMESTAMP returns a timestamp (epoche value).
  • The latitude (Northern latitude; use negative values for a Southern latitude)
  • The longitude (Eastern longitude; use negative values for a Western longitude)
  • The zenith of the sunrise (in degrees)
  • The offset (in hours) to Coordinated Universal Time or UTC.
<?php 
 $current_time_stamp = time();
 $sunrise = date_sunrise($current_time_stamp);
 $sunset  = date_sunset($current_time_stamp);
 
 echo 'Sunrise: ', $sunrise , '<br>';
 //Sunrise: 05:33
 
 echo 'Sunset: ', $sunset;
 //Sunset  16:09;

So, the preceding code calculates the sunrise and sunset for the current day. Visit https://php.net/manual/function.date-sunrise.php and https://php.net/manual/function.date-sunset.php for more information.

Getting sunset, sunrise, twilight, and transit information with date_sun_info()

<?php
 //Syntax
 date_sun_info(int $timestamp, float $latitude, float $longitude): array

This function takes three parameters: Unix timestamp (epoche), latitude, and longitude, it returns an associative array containing information about sunset/sunrise and twilight begin/end for a specified day and location. The returned array contains following keys:

  • sunrise – the timestamp of the sunrise
  • sunset – the timestamp of the sunset
  • transit – the timestamp when the sun is at its zenith
  • civil_twilight_begin – the timestamp civil twilight begins
  • civil_twilight_end – the timestamp civil twilight ends
  • nautical_twilight_begin – the timestamp nautical twilight begins
  • nautical_twilight_end – the timestamp nautical twilight ends
  • astronomical_twilight_begin – the timestamp astronomical twilight begins
  • astronomical_twilight_end – the timestamp astronomical twilight ends

Example: Using date_sun_info() for America/Los_Angeles Timezone:

<?php 
 date_default_timezone_set('America/Los_Angeles');
 
 $date      = new DateTime();
 $timestamp = $date->getTimestamp();

 $timezone  = $date->getTimezone();
 $location  = $timezone->getLocation();

 $latitude  = $location['latitude'];
 $longitude = $location['longitude'];
 
 $info = date_sun_info($timestamp,$latitude,$longitude);
 
 foreach ($info as $k => $v)
  echo $k.': '. Date('d M Y, H:i:s',$v).'<br>';
  
  /*Prints:
sunrise: 27 Jan 2023, 06:51:49
sunset: 27 Jan 2023, 17:19:36
transit: 27 Jan 2023, 12:05:43
civil_twilight_begin: 27 Jan 2023, 06:26:41
civil_twilight_end: 27 Jan 2023, 17:44:44
nautical_twilight_begin: 27 Jan 2023, 05:56:31
nautical_twilight_end: 27 Jan 2023, 18:14:54
astronomical_twilight_begin: 27 Jan 2023, 05:26:52
astronomical_twilight_end: 27 Jan 2023, 18:44:33 */

The Date and Time Tutorials: