如何计算月球数月相

时间:2018-02-10 16:55:28

标签: php astronomy

如何计算给定日期的月号

我尝试了很多方法但输出错误

方法1

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

输出为999但2003年10月25日的月球数为1000

方法2产生相同的结果

var createTable = "<table><thead><tr id='keysRow'></tr></thead>";
var endHead = "<tbody><tr id='valuesRow'></tr></tbody></table>";  

//More code that's connecting to each other and displays it in a div.
//F12 is showing me that this works.

help from wikipedia

lunations numbers table

2 个答案:

答案 0 :(得分:1)

输出是正确的,只是偏移一个。如果你在1923年1月17日的第一次月球时尝试你的公式,你就会得到0.你可以在其他几个日期尝试它,你总能得到正确的一天短暂的1。

所以只需添加1即可获得正确的结果:

<?php
function getLunationNumber($m, $d, $y)
{
    $offset = 1;
    $synodicMonth = 29.530588861;
    $lunationBase = 2423436.40347;
    return round(((gregoriantojd($m, $d, $y) - $lunationBase ) / $synodicMonth )) + $offset;
}
echo getLunationNumber(1, 17, 1923).PHP_EOL; // 1
echo getLunationNumber(10, 25, 2003).PHP_EOL; // 1000

Demo

答案 1 :(得分:0)

这是我放在一起的一个小函数,它将给你,日期,月亮和月相(0到1)作为一个数组,它与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

<强>结果:

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
)