Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何计算月相数_Php_Astronomy - Fatal编程技术网

Php 如何计算月相数

Php 如何计算月相数,php,astronomy,Php,Astronomy,如何从给定日期计算月数 我尝试了许多方法,但仍然输出错误 方法1 $SYNODIC_MONTH = 29.530588861; $LUNATION_BASE = 2423436.40347; echo round(( (gregoriantojd (10,25,2003) - $LUNATION_BASE ) / $SYNODIC_MONTH )); 输出为999,但2003年10月25日的月朔数为1000 方法2产生相同的结果 $now =strtotime('20

如何从给定日期计算月数

我尝试了许多方法,但仍然输出错误

方法1

$SYNODIC_MONTH = 29.530588861;
     $LUNATION_BASE = 2423436.40347; 
     echo   round(( (gregoriantojd (10,25,2003) - $LUNATION_BASE ) / $SYNODIC_MONTH ));
输出为999,但2003年10月25日的月朔数为1000

方法2产生相同的结果

$now =strtotime('2003-10-25');
           $diff =$now-strtotime('1923-1-17'); 
            $days= round($diff/86400);
       echo '<br>'. round($lunations = 0.20439731 + $days * 0.03386319269);
$now=strotime('2003-10-25');
$diff=$now strotime('1923-1-17');
$days=整数($diff/86400);
回音“
”。四舍五入($lunations=0.20439731+$days*0.03386319269);


输出是正确的,只是偏移了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

这里是我组合的一个小函数,它将把日期、月朔和月相(0到1)作为一个数组,与以下位置的结果匹配:

<?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);
}
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
)