Search code examples
phpastronomy

how to calculate lunation number moon phases


how to calculate lunation number from given date

I have tried many methods but still wrong output

Method 1

$SYNODIC_MONTH = 29.530588861;
     $LUNATION_BASE = 2423436.40347; 
     echo   round(( (gregoriantojd (10,25,2003) - $LUNATION_BASE ) / $SYNODIC_MONTH ));

an output is 999 but on 25 October 2003 lunation number is 1000

method 2 produce the same result

$now =strtotime('2003-10-25');
           $diff =$now-strtotime('1923-1-17'); 
            $days= round($diff/86400);
       echo '<br>'. round($lunations = 0.20439731 + $days * 0.03386319269);

help from wikipedia

lunations numbers table


Solution

  • Here is a little function i've put together which will give you, date, lunation and moon phase (0 to 1) as an array, which matches with the results at: http://scienceworld.wolfram.com/astronomy/Lunation.html.

    <?php
    
    function moon($time = null, $mps = 2551443) {
    
        $time = is_null($time) ? 0 : (time() - $time);
        $first_moon = time()-mktime(0, 0, 0, 0, 0, 1923); // first moon (1923)
    
        date_default_timezone_set("UTC");
        return [
            'date' => date_create_from_format('U', time()-$time)->format('Y-m-d H:i:s'),
            'lunation' => ($first_moon-$time)/$mps,
            'phase' => ($time - $mps * intval($time / $mps)) / $mps
        ];
    }
    
    
    /**
     * Test - loop  over each + 29 days, 12 hours, 44 mins, 3 secs
     */
    $mps = 2551443; // moon phase in seconds (29 days, 12 hours, 44 mins, 3 secs)
    
    $test = function () use ($mps) {
        $begin = new DateTime('2001-01-24');
        $end = new DateTime('2004-12-12');
    
        $dtia = date_create_from_format('U', 0);
        $dtib = date_create_from_format('U', $mps);
    
        $interval = new DateInterval('P'.$dtia->diff($dtib)->format('%aDT%hH%iM%sS'));
        $daterange = new DatePeriod($begin, $interval ,$end);
    
        foreach($daterange as $date){
            yield moon($date->format('U'));
        }
    };
    
    foreach ($test() as $result) {
        print_r($result);
    }
    

    https://3v4l.org/HI6Lv

    Result:

    Array
    (
        [date] => 2001-01-23 23:00:00
        [lunation] => 966.62381718894
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2001-02-22 11:44:03
        [lunation] => 967.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2001-03-24 00:28:06
        [lunation] => 968.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2001-04-22 12:12:09
        [lunation] => 969.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2001-05-22 00:56:12
        [lunation] => 970.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2001-06-20 13:40:15
        [lunation] => 971.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2001-07-20 02:24:18
        [lunation] => 972.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2001-08-18 15:08:21
        [lunation] => 973.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2001-09-17 03:52:24
        [lunation] => 974.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2001-10-16 16:36:27
        [lunation] => 975.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2001-11-15 06:20:30
        [lunation] => 976.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2001-12-14 19:04:33
        [lunation] => 977.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2002-01-13 07:48:36
        [lunation] => 978.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2002-02-11 20:32:39
        [lunation] => 979.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2002-03-13 09:16:42
        [lunation] => 980.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2002-04-11 21:00:45
        [lunation] => 981.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2002-05-11 09:44:48
        [lunation] => 982.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2002-06-09 22:28:51
        [lunation] => 983.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2002-07-09 11:12:54
        [lunation] => 984.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2002-08-07 23:56:57
        [lunation] => 985.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2002-09-06 12:41:00
        [lunation] => 986.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2002-10-06 01:25:03
        [lunation] => 987.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2002-11-04 15:09:06
        [lunation] => 988.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2002-12-04 03:53:09
        [lunation] => 989.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2003-01-02 16:37:12
        [lunation] => 990.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2003-02-01 05:21:15
        [lunation] => 991.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2003-03-02 18:05:18
        [lunation] => 992.62335784103
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2003-04-01 05:49:21
        [lunation] => 993.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2003-04-30 18:33:24
        [lunation] => 994.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2003-05-30 07:17:27
        [lunation] => 995.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2003-06-28 20:01:30
        [lunation] => 996.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2003-07-28 08:45:33
        [lunation] => 997.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2003-08-26 21:29:36
        [lunation] => 998.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2003-09-25 10:13:39
        [lunation] => 999.62194687477
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2003-10-24 22:57:42
        [lunation] => 1000.6219468748
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2003-11-23 12:41:45
        [lunation] => 1001.623357841
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2003-12-23 01:25:48
        [lunation] => 1002.623357841
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2004-01-21 14:09:51
        [lunation] => 1003.623357841
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2004-02-20 02:53:54
        [lunation] => 1004.623357841
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2004-03-20 15:37:57
        [lunation] => 1005.623357841
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2004-04-19 03:22:00
        [lunation] => 1006.6219468748
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2004-05-18 16:06:03
        [lunation] => 1007.6219468748
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2004-06-17 04:50:06
        [lunation] => 1008.6219468748
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2004-07-16 17:34:09
        [lunation] => 1009.6219468748
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2004-08-15 06:18:12
        [lunation] => 1010.6219468748
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2004-09-13 19:02:15
        [lunation] => 1011.6219468748
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2004-10-13 07:46:18
        [lunation] => 1012.6219468748
        [phase] => 0.86067883938618
    )
    Array
    (
        [date] => 2004-11-11 21:30:21
        [lunation] => 1013.623357841
        [phase] => 0.85926787312121
    )
    Array
    (
        [date] => 2004-12-11 10:14:24
        [lunation] => 1014.623357841
        [phase] => 0.85926787312121
    )