gregseth / suncalc-php

PHP library for basic astronomical computation (sun and moon rise and set, moon phases, …)
GNU General Public License v2.0
65 stars 20 forks source link

Wrong date in European timezone results #15

Open ottopic opened 5 years ago

ottopic commented 5 years ago

Hello @gregseth , thank you for your great work. I'm testing your script but I have strange problem.

If I try to calculate sunTimes with:

datetime : 2019-02-09 00:01:00 latitude: 43.7102466075706 longitude: 11.2609452326665

in results I find the day before 2019-02-08

$sun_info:
Array
(
    [solarNoon] => DateTime Object
        (
            [date] => 2019-02-08 12:30:28.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [nadir] => DateTime Object
        (
            [date] => 2019-02-08 00:30:28.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [sunrise] => DateTime Object
        (
            [date] => 2019-02-08 07:25:21.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [sunset] => DateTime Object
        (
            [date] => 2019-02-08 17:35:35.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [sunriseEnd] => DateTime Object
        (
            [date] => 2019-02-08 07:28:30.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [sunsetStart] => DateTime Object
        (
            [date] => 2019-02-08 17:32:26.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [dawn] => DateTime Object
        (
            [date] => 2019-02-08 06:55:19.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [dusk] => DateTime Object
        (
            [date] => 2019-02-08 18:05:37.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [nauticalDawn] => DateTime Object
        (
            [date] => 2019-02-08 06:21:19.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [nauticalDusk] => DateTime Object
        (
            [date] => 2019-02-08 18:39:37.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [nightEnd] => DateTime Object
        (
            [date] => 2019-02-08 05:47:53.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [night] => DateTime Object
        (
            [date] => 2019-02-08 19:13:04.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [goldenHourEnd] => DateTime Object
        (
            [date] => 2019-02-08 08:06:44.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

    [goldenHour] => DateTime Object
        (
            [date] => 2019-02-08 16:54:12.000000
            [timezone_type] => 3
            [timezone] => Europe/Rome
        )

)

I tried to edit your script passing right timezone to "fromJulian" function but never change. Can you help me?

ottopic commented 5 years ago

@gregseth using UTC date time and converting it in current timezone the result it seems to works but only in my current European timezone, if I try with U.S.A latitude and longitude it returns wrong day.

ottopic commented 5 years ago

This is the workaround I have found to calculate correct date time, but I don't think it's a good job, someone can help me?

$DateTimeZone = new DateTimeZone($timeZone->timeZoneId);
$DateTimeZoneUTC = new DateTimeZone("UTC");

if(strpos($timeZone->timeZoneId, "Europe") !== false) $DateTimeZone = $DateTimeZoneUTC;

$newSunData = new AurorasLive\SunCalc(new DateTime($date, $DateTimeZone), $lat, $lng);
su1736 commented 3 years ago

If I try to calculate sunTimes with:

datetime : 2019-02-09 00:01:00 latitude: 43.7102466075706 longitude: 11.2609452326665

in results I find the day before 2019-02-08

I also faced with this problem. I don't found right fix or workaround for this yet.

pixlmint commented 1 year ago

I have encountered this same issue and found that modifying this line to read $d = toDays($this->date) + 0.5; fixes the issue

If you look at the toJulian function, there 0.5 gets subtracted from the result, I believe that means it removes half a day from the time, and if you're using 00:00 as time from which to calculate the sun/moon times, then remove half a day, the previous day will be selected.

This seems to be working for me, but certainly needs more testing.

Comparing the results this gives me to what timeanddate is telling me, the sun times only have a variation of +/- 5 minutes. Though you cannot add the 0.5 to the moontimes algorithm, as that will mess things up.

This was tested at 5 different coordinates with 20 random dates

PendalF89 commented 2 months ago

@pixlmint

I have encountered this same issue and found that modifying this line to read $d = toDays($this->date) + 0.5; fixes the issue

Unfortunately, no. Please, check this comment: https://github.com/mourner/suncalc/issues/107#issuecomment-362287177

Here is my implementation on PHP:

function toJulian($date): float
{
    $month = $date->format('m');
    $day = $date->format('d');
    $year = $date->format('Y');

    $gregorian = !($year < 1583);

    if ($month == 1 || $month == 2) {
        $year -= 1;
        $month += 12;
    }

    $a = floor($year / 100);

    if ($gregorian) {
        $b = 2 - $a + floor($a / 4);
    } else {
        $b = 0.0;
    }

    $jd = floor(365.25 * ($year + 4716))
        + floor(30.6001 * ($month + 1))
        + $day + $b - 1524.5;

    return $jd + 0.5;
}