PHPackages                             jiri.jozif/datesuninfo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jiri.jozif/datesuninfo

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jiri.jozif/datesuninfo
======================

PHP library for calculating sunrise and sunset

1.0.4(1y ago)014MITPHPPHP &gt;=8.1

Since Apr 10Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/JiriJozif/datesuninfo)[ Packagist](https://packagist.org/packages/jiri.jozif/datesuninfo)[ RSS](/packages/jirijozif-datesuninfo/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

[![DateSunInfo](https://raw.githubusercontent.com/JiriJozif/datesuninfo/main/sun.png)](https://raw.githubusercontent.com/JiriJozif/datesuninfo/main/sun.png)

PHP library for calculating Sunrise, Sunset and Sun transit
===========================================================

[](#php-library-for-calculating-sunrise-sunset-and-sun-transit)

A more accurate replacement for the PHP function `date_sun_info()`. In addition, the library provides extra values and functions.

Installation
------------

[](#installation)

This library is available for use with [Composer](https://packagist.org/packages/jiri.jozif/datesuninfo) — add it to your project by running:

```
$ composer require jiri.jozif/datesuninfo
```

Usage
-----

[](#usage)

The replacement function is simple, instead of

```
$dsi = date_sun_info($timestamp, $latitude, $longitude);

```

you type

```
use JiriJozif\DateSunInfo\DateSunInfo;

$dsi = DateSunInfo::get($timestamp, $latitude, $longitude);

```

The class returns the same data as date\_sun\_info(), but with more accurate values.

Static class method DateSunInfo::get with up to three required parameters; the fourth is optional:

- **$timestamp** (int): The Unix timestamp.
- **$latitude** (float): The latitude of the location in degrees.
- **$longitude** (float): The longitude of the location in degrees.
- **$asObject** (bool, optional): The default output is an array. If the fourth argument asObject is set to `true`, the output will be an object. Example:

```
$dsi = DateSunInfo::get($timestamp, $latitude, $longitude, true);
echo date('H:i', $dsi->sunrise);

```

You can then use the following methods:

- `DateSunInfo::get(int $timestamp, float $latitude, float $longitude, ?bool $asObject)`: Returns the same output as the date\_sun\_info() function, but with more accurate values.
- `DateSunInfo::getRiset(int $timestamp, float $latitude, float $longitude, ?bool $asObject)`: Shorter output, returning only the sunset, sunrise, and sun transit times.
- `DateSunInfo::getAll(int $timestamp, float $latitude, float $longitude, ?bool $asObject)`: Extended output, returning the azimuth of sunset and sunrise, as well as the height of the sun transit.
- `DateSunInfo::getPosition(int $timestamp, ?float $latitude, ?float $longitude, ?bool $asObject)`: Returns the sun's coordinates for the given timestamp. If latitude and longitude are specified, it also returns the horizon coordinates.
- `DateSunInfo::hh_mm(int $timestamp)`: Returns the rounded time (to the nearest minute) as a string in hh:mm format, or "\*\*:\*\*" if the sun is continuously above the horizon, or "--:--" if the sun is continuously below the horizon.
- `DateSunInfo::hh_mm_ss(int $timestamp)`: Returns the time as a string in hh:mm:ss format, or "\*\*:\*\*:\*\*" if the sun is continuously above the horizon, or "--:--:--" if the sun is continuously below the horizon.

Return Values:

- `sunrise`: The timestamp of the sunrise or `true` is Sun continuously above horizon or `false` if Sun continuously below horizon.
- `sunset`: The timestamp of the sunset or `true` is Sun continuously above horizon or `false` if Sun continuously below horizon.
- `transit`: The timestamp when the Sun is at its zenith, i.e. has reached its topmost point.
- `civil_twilight_begin`: The start of the civil dawn (The Sun is 6° below the horizon). It ends at sunrise.
- `civil_twilight_end`: The end of the civil dusk (The Sun is 6° below the horizon). It starts at sunset.
- `nautical_twilight_begin`: The start of the nautical dawn (The Sun is 12° below the horizon). It ends at civil\_twilight\_begin.
- `nautical_twilight_end`: The end of the nautical dusk (The Sun is 12° below the horizon). It starts at civil\_twilight\_end.
- `astronomical_twilight_begin`: The start of the astronomical dawn (The Sun is 18° below the horizon). It ends at nautical\_twilight\_begin.
- `astronomical_twilight_end`: The end of the astronomical dusk (The Sun is 18° below the horizon). It starts at nautical\_twilight\_end.

Extended output from method DateSunInfo::getAll():

- `sunrise_azimuth`: The azimuth of the sunrise in degree.
- `sunset_azimuth`: The azimuth of the sunset in degree.
- `transit_height`: The height of the transit in degree.
- `lowest_height`: The lowest height of the Sun in the sky, in degrees, occurs on the opposite side as the transit.

Output from method DateSunInfo::getPosition():

- `RA`: Right Ascension in degree.
- `Dec`: Declination in degree.
- `Lon`: Ecliptic longitude in degree.
- `R`: Distance from Earth in AU.
- `h`: Horizontal coordinate system, height (altitude) above horizon in degree (only if latitude and longitude were entered).
- `Az`: Horizontal coordinate system, azimuth in degree (only if latitude and longitude were entered).

### Example

[](#example)

```
