如何计算农历月相
how to calculate lunation number moon phases
如何从给定日期计算农历月数
尝试了很多方法还是输出错误
方法一
$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);
输出是正确的,只是偏移了一个。如果您在 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),与 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);
}
结果:
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
)
如何从给定日期计算农历月数
尝试了很多方法还是输出错误
方法一
$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);
输出是正确的,只是偏移了一个。如果您在 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),与 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);
}
结果:
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
)