w3resource

PHP : date_sunset() function

Description

The date_sunset() function returns the time of sunset for a given day and location.

Version:

(PHP 4 and above)

Syntax:

date_sunset(timestamp, format, latitude, longitude, zenith, gmt_offset) 

Parameters:

Name Description Required /
Optional
Type
timestamp The timestamp of the day. Required Integer
format Sets the format of the output.

Use SUNFUNCS_RET_STRING if you want the output as string (example 16:46)

Use UNFUNCS_RET_DOUBLE if you want the output as float (example 16.78243132)

Use SUNFUNCS_RET_TIMESTAMP if you want the output as integer (example 1095034606)
Optional Integer
latitude Indicates the latitude of a location. Use a negative value for South. The default is North. Optional Float
longitude Indicates the longitude of a location. Use a negative value for West. The default is East. Optional Float
zenith Solar zenith angle. Optional Float
gmt_offset Difference between GMT and local time in hours. For example, since the difference between GMT and Calcutta-India is five and half hours, therefore for IST, use 5.50. Optional Float

Return value:

The sunset time in a specified format on success, or FALSE on failure.

Value Type: Mixed*.

*Mixed : Mixed indicates multiple (but not necessarily all) types.

Example :

<?php
//Calculate the sunrise time for Kolkata, India
//Latitude: 22.34 North
//Longitude: 88.24 East
//Zenith ~= 45.6177°
//offset: +5.50 GMT
echo("Date: " . date("D M d Y") . "<br />");
echo("Sunset time: ");
echo(date_sunset(time(),SUNFUNCS_RET_STRING,22.34,88.24,90.5546,5.50));
?>

View the example in the browser

See also

PHP Function Reference

Previous: date_sunrise
Next: date



PHP: Tips of the Day

Getting all defined constants

To get all defined constants including those created by PHP use the get_defined_constants function:

Example:

<?php
$constants = get_defined_constants();
var_dump($constants); // pretty large list

Output:

array(2250) {
  ["E_ERROR"]=>
  int(1)
  ["E_RECOVERABLE_ERROR"]=>
  int(4096)
  ["E_WARNING"]=>
  .....
  .....
  resource(1) of type (stream)
  ["STDOUT"]=>
  resource(2) of type (stream)
  ["STDERR"]=>
  resource(3) of type (stream)
}

To get only those constants that were defined by your app call the function at the beginning and at the end of your script (normally after the bootstrap process):

<?php
$constants = get_defined_constants();
define("HELLO", "hello");
define("WORLD", "world");
$new_constants = get_defined_constants();
$myconstants = array_diff_assoc($new_constants, $constants);
var_export($myconstants);

Output:

array (
  'HELLO' => 'hello',
  'WORLD' => 'world',
)

It's sometimes useful for debugging