PHP | date_sun_info() Function
The date_sun_info() is an inbuilt function in PHP which is used to find the information about sunset/sunrise and twilight begin/end for a specified day and location.
Syntax:
array date_sun_info($timestamp, $latitude, $longitude)
Parameters: This function accepts three parameters as mentioned above and described below:
- $timestamp: It is a mandatory parameter which specifies the timestamp of the day from which the sunrise time is taken.
- $latitude: It is an mandatory parameter which specifies the latitude of the location. By default, it set as North. To specify a value for South, pass in a negative value.
- $longitude: It is an mandatory parameter which specifies the longitude of the location. By defaults, it set as East. To modify a value for West, pass in a negative value.
Return Value: It returns an array containing information about sunset/sunrise and twilight begin/end, for a specified day and location and returns False on failure.
Exceptions: In PHP versions 5.2.2 the order of parameters $latitude and $longitude has been swapped.
Below programs illustrate the date_sun_info() function.
Program 1:
php
<?php // PHP program to print information // about sunset/sunrise and twilight // begin/end for specified location // New Delhi India /* ********New Delhi******** Latitude = 28.6139° N Longitude = 77.2090° E */ $arr = date_sun_info( strtotime ( "June-26-2018" ), 28.61, 77.2090 ); foreach ( $arr as $key => $val ) { echo "$key: " . date ( "H:i:s" , $val ) . "\n" ; } ?> |
Output:
sunrise: 23:55:58 sunset: 13:53:02 transit: 06:54:30 civil_twilight_begin: 23:29:08 civil_twilight_end: 14:19:52 nautical_twilight_begin: 22:56:35 nautical_twilight_end: 14:52:25 astronomical_twilight_begin: 22:21:59 astronomical_twilight_end: 15:27:01
Program 2:
php
<?php // PHP program to print information // about sunset/sunrise and twilight // begin/end for specified location // USA Washington, D.C. // Latitude = 38.9072° N // Longitude = 77.0369° W $arr = date_sun_info( strtotime ( "June-26-2018" ), 38.9072, 77.0369 ); foreach ( $arr as $key => $val ) { echo "$key: " . date ( "H:i:s" , $val ) . "\n" ; } ?> |
Output:
sunrise: 23:28:58 sunset: 14:21:24 transit: 06:55:11 civil_twilight_begin: 22:57:03 civil_twilight_end: 14:53:20 nautical_twilight_begin: 22:16:45 nautical_twilight_end: 15:33:38 astronomical_twilight_begin: 21:30:31 astronomical_twilight_end: 16:19:51
Related Articles:
Reference: http://php.net/manual/en/function.date-sun-info.php
Contact Us