English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_sun_info() Function Usage and Example

PHP Date & Time Function Manual

The date_sun_info() function returns an array containing information about sunrise/Sunset and Dusk Begin/at the given location in an array of information about dusk end.

Definition and Usage

The date_sun_info() function accepts the time, latitude, and longitude of the location and provides information about sunrise/Sunset and Dusk Begin/End of information.

Syntax

date_sun_info($timestamp, $latitude, $longitude)

Parameter

Serial NumberParameters and Description
1

timestamp (required)

This specifies a timestamp.

2

latitude (required)

This specifies the latitude of the location.

3

longitude (required)

This specifies the longitude of the specified location.

Return value

date_sun_info() function returns an array containing information about the sunrise/Sunset and dusk start/End of information.

PHP version

This function was originally introduced in PHP version5.2.0 introduced and can be used in all higher versions.

Online example

The following examples demonstratedate_sun_info()Function usage-

<?php
   $sun_info = date_sun_info("02-17-2012", 20.5937, 78.9629);
   print_r($sun_info);
?>
Test and see‹/›

Output result

Array
(
    [sunrise] => 4818
    [sunset] => 44087
    [transit] => 24453
    [civil_twilight_begin] => 3381
    [civil_twilight_end] => 45524
    [nautical_twilight_begin] => 1729
    [nautical_twilight_end] => 47176
    [astronomical_twilight_begin] => 98
    [astronomical_twilight_end] => 48807
)

Online example

The following example retrieves information on different locations on the same date-

<?php
   $sun_info = date_sun_info("02-17-2012", 37.0902, 95.7129);
   print_r($sun_info);
?>
Test and see‹/›

Output result

Array
(
    [sunrise] => 3038
    [sunset] => 37825
    [transit] => 20431
    [civil_twilight_begin] => 1307
    [civil_twilight_end] => 39556
    [nautical_twilight_begin] => -642
    [nautical_twilight_end] => 41505
    [astronomical_twilight_begin] => -2538
    [astronomical_twilight_end] => 43402
)

Online example

The following example retrieves location information on different dates-

<?php
   $time = "2000-01-01";
   $latitude = 31.7667;
   $longitude = 35.2333;
   print_r(date_sun_info($time, $latitude, $longitude));
   $time = "2010-01-01";
   print_r(date_sun_info($time, $latitude, $longitude));   
   $time = "2020-01-01";
   print_r(date_sun_info($time, $latitude, $longitude));
?>
Test and see‹/›

Output result

Array
(
    [sunrise] => 16742
    [sunset] => 53161
    [transit] => 34951
    [civil_twilight_begin] => 15138
    [civil_twilight_end] => 54765
    [nautical_twilight_begin] => 13316
    [nautical_twilight_end] => 56587
    [astronomical_twilight_begin] => 11534
    [astronomical_twilight_end] => 58369
)
Array
(
    [sunrise] => 16742
    [sunset] => 53161
    [transit] => 34951
    [civil_twilight_begin] => 15138
    [civil_twilight_end] => 54765
    [nautical_twilight_begin] => 13316
    [nautical_twilight_end] => 56587
    [astronomical_twilight_begin] => 11534
    [astronomical_twilight_end] => 58369
)
Array
(
    [sunrise] => 16742
    [sunset] => 53161
    [transit] => 34951
    [civil_twilight_begin] => 15138
    [civil_twilight_end] => 54765
    [nautical_twilight_begin] => 13316
    [nautical_twilight_end] => 56587
    [astronomical_twilight_begin] => 11534
    [astronomical_twilight_end] => 58369
)

Online example

<?php
   $sun_info = date_sun_info(strtotime("2017-07-12), 20.5937, 78.9629);
   foreach ($sun_info as $key => $val) {
      echo "$key:  " . date("H:i:s", $val) . "\n";
   }
?>
Test and see‹/›

Output result

sunrise: 00:11:03
sunset: 13:28:33
transit: 06:49:48
civil_twilight_begin: 23:46:45
civil_twilight_end: 13:52:51
nautical_twilight_begin: 23:17:48
nautical_twilight_end: 14:21:47
astronomical_twilight_begin: 22:47:55
astronomical_twilight_end: 14:51:41