PHP | date_sun_info() Function

PHP date_sun_info() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_sun_info() Function.

The PHP date_sun_info() function accepts time, latitudes and longitudes of a location and then gives information about sunrise, sunset, begin, and end of the twilight, in the given location.

Syntax

Following below is the syntax to use this function -

date_sun_info($timestamp, $latitude, $longitude)


Parameter Details

Sr.NoParameter & Description
1

timestamp (Mandatory)

This specifies a timestamp.

2

latitude (Mandatory)

This specifies latitude of a location.

3

longitude (Mandatory)

This specifies longitude of a location.


Return Value

This built-in PHP function returns an array containing the information about sunrise, sunset, begin and end of the twilight for the given day in the specified location.

PHP Version

This function was first introduced as part of core PHP v 5.2.0 and, works with all the later versions.

Example1

Following example illustrates the usage of the PHP date_sun_info() function -

<?php
   $sun_info = date_sun_info("02-17-2012", 20.5937, 78.9629);
   print_r($sun_info);
?>

Output

When the above code is executed, it will produce the following 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
)

Example2

The following example gets the info on the same date at various locations -

<?php
   $sun_info = date_sun_info("02-17-2012", 37.0902, 95.7129);
   print_r($sun_info);
?>

Output

When the above code is executed, it will produce the following 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
)

Example3

Following example get the info at a location in 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));
?>

Output

When the above code is executed, it will produce the following 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
)

Example4

Try the following 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";
   }
?>

Output

When the above code is executed, it will produce the following 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


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP date_sunrise() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.