The date_sunset function in PHP is used to retrieve the sunset time for a specific date and location. It calculates the sunset time based on the given latitude, longitude, and date. Typically, this function returns a Unix timestamp, which is the number of seconds from January 1, 1970, to the specified time.
The basic syntax of the date_sunset function is as follows:
<span><span><span class="hljs-title function_ invoke__">date_sunset</span></span><span>(time, format, latitude, longitude, zenith, gmt_offset);
</span></span>
time: Optional, a timestamp, defaults to the current time.
format: The result format. Can be SUNFUNCS_RET_TIMESTAMP (Unix timestamp, default), SUNFUNCS_RET_STRING (returns a time string like '6:00 PM'), or SUNFUNCS_RET_DOUBLE (returns the sunset angle).
latitude and longitude: Required, specify the latitude and longitude of the sunset location.
zenith: Optional, the zenith angle of the sun, usually 90.5 degrees, representing the sun's position at sunset.
gmt_offset: Optional, time zone offset, defaults to 0.
SUNFUNCS_RET_TIMESTAMP (default)
If the format parameter is set to SUNFUNCS_RET_TIMESTAMP (or omitted), the function will return a Unix timestamp, which is the number of seconds from midnight January 1, 1970, to the time of sunset.
Example:
<span><span><span class="hljs-variable">$sunset</span></span><span> = </span><span><span class="hljs-title function_ invoke__">date_sunset</span></span><span>(</span><span><span class="hljs-title function_ invoke__">time</span></span><span>(), SUNFUNCS_RET_TIMESTAMP, </span><span><span class="hljs-number">40.7128</span></span><span>, -</span><span><span class="hljs-number">74.0060</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$sunset</span></span><span>;
</span></span>
The returned value will be a number representing the Unix timestamp.
SUNFUNCS_RET_STRING
If the format parameter is set to SUNFUNCS_RET_STRING, a formatted time string is returned, usually in 12-hour format (e.g., '6:00 PM').
Example:
<span><span><span class="hljs-variable">$sunset</span></span><span> = </span><span><span class="hljs-title function_ invoke__">date_sunset</span></span><span>(</span><span><span class="hljs-title function_ invoke__">time</span></span><span>(), SUNFUNCS_RET_STRING, </span><span><span class="hljs-number">40.7128</span></span><span>, -</span><span><span class="hljs-number">74.0060</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$sunset</span></span><span>;
</span></span>
The output might look like 6:30 PM, indicating the sunset time in New York City.
SUNFUNCS_RET_DOUBLE
If the format parameter is set to SUNFUNCS_RET_DOUBLE, it returns a floating-point value representing the sun's altitude angle. The value is usually negative, meaning the sun is below the horizon, and the larger the absolute value, the lower the sun is.
Example:
<span><span><span class="hljs-variable">$sunset</span></span><span> = </span><span><span class="hljs-title function_ invoke__">date_sunset</span></span><span>(</span><span><span class="hljs-title function_ invoke__">time</span></span><span>(), SUNFUNCS_RET_DOUBLE, </span><span><span class="hljs-number">40.7128</span></span><span>, -</span><span><span class="hljs-number">74.0060</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$sunset</span></span><span>;
</span></span>
The returned value might be a float like -0.934, indicating the sun's angle below the horizon.
Since the result returned by date_sunset can be in different formats, the correct parsing method depends on the format parameter you use.
If you use SUNFUNCS_RET_TIMESTAMP, the result is a Unix timestamp, which can be converted to a readable date and time using the date function.
Example:
<span><span><span class="hljs-variable">$sunset_timestamp</span></span><span> = </span><span><span class="hljs-title function_ invoke__">date_sunset</span></span><span>(</span><span><span class="hljs-title function_ invoke__">time</span></span><span>(), SUNFUNCS_RET_TIMESTAMP, </span><span><span class="hljs-number">40.7128</span></span><span>, -</span><span><span class="hljs-number">74.0060</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">"Y-m-d H:i:s"</span></span><span>, </span><span><span class="hljs-variable">$sunset_timestamp</span></span><span>);
</span></span>
This will output a standard date-time format, such as 2025-06-16 19:45:00.
If you use SUNFUNCS_RET_STRING, the result is a formatted time string, which you can directly display or further process.
Example:
<span><span><span class="hljs-variable">$sunset_string</span></span><span> = </span><span><span class="hljs-title function_ invoke__">date_sunset</span></span><span>(</span><span><span class="hljs-title function_ invoke__">time</span></span><span>(), SUNFUNCS_RET_STRING, </span><span><span class="hljs-number">40.7128</span></span><span>, -</span><span><span class="hljs-number">74.0060</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The sunset time is: "</span></span><span> . </span><span><span class="hljs-variable">$sunset_string</span></span><span>;
</span></span>
This will output something like The sunset time is: 6:30 PM.
If you use SUNFUNCS_RET_DOUBLE, the returned floating value represents the sun's altitude angle. This value is usually negative, indicating the sun has already set.
Example:
<span><span><span class="hljs-variable">$sunset_angle</span></span><span> = </span><span><span class="hljs-title function_ invoke__">date_sunset</span></span><span>(</span><span><span class="hljs-title function_ invoke__">time</span></span><span>(), SUNFUNCS_RET_DOUBLE, </span><span><span class="hljs-number">40.7128</span></span><span>, -</span><span><span class="hljs-number">74.0060</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Sunset angle: "</span></span><span> . </span><span><span class="hljs-variable">$sunset_angle</span></span><span>;
</span></span>
The return value might be a decimal like -0.934, representing the sun’s angle.
The date_sunset function is very useful, especially when you need to get sunset times based on a location’s latitude and longitude. The data format it returns depends on the value of the format parameter—it can be a Unix timestamp, a formatted time string, or the sun’s altitude angle. To correctly parse the return value, you need to choose the appropriate conversion or display method based on the selected format.
By mastering these basic usages, you can more easily handle sunset-related data in PHP. Whether it’s for sunset alerts, weather forecasting, or astronomical calculations, it can be a valuable tool.