gregoriantojd

(PHP 4, PHP 5, PHP 7, PHP 8)

gregoriantojd将公历日期转为儒略日数

说明

gregoriantojd(int $month, int $day, int $year): int

公历的有效范围是从公元前 4714 年 11 月 25 日开始。至少到公元 9999 年 12 月 31 日

虽然这个函数可以处理追溯到公元前 4714 年以前的日期,但这样做可能没有意义。公历直到 1582 年 10 年 15 日(或是儒略历 1582 年 10 月 5 日)才被发明,一些国家直到很久以后才接受它。比如,英国是在 1752 年开始使用公历,苏联是在 1918 年,希腊是在 1923 年,大部分的欧洲国家在公历前使用儒略历。

参数

month

月份的范围是 1(January)到 12(December)之间的数字。

day

天的范围是 1 到 31 之间的数字。如果该月的天数小于指定的天数, 则会发生溢出。请参考下面的示例。

year

年份的范围是 -4714 到 9999 之间的数字。负数表示公元前,正数表示公元。注意没有公元 0 年,公元前 1 年 12 月 31 日紧跟其后的是公元 1 年 1 月 1 日。

返回值

int 类型的指定公历日期的儒略日。日期超出范围值返回 0

示例

示例 #1 历法函数

<?php
$jd
= gregoriantojd(10, 11, 1970);
echo
"$jd\n";
$gregorian = jdtogregorian($jd);
echo
"$gregorian\n";
?>

以上示例会输出:

2440871
10/11/1970

示例 #2 溢出行为

<?php
echo gregoriantojd(2, 31, 2018), PHP_EOL,
gregoriantojd(3, 3, 2018), PHP_EOL;
?>

以上示例会输出:

2458181
2458181

参见

add a note

User Contributed Notes 3 notes

up
2
jettyrat at jettyfishing dot com
19 years ago
You can obtain the decimal fraction of the Julian date with the php gregoriantojd() function or the function shown below by applying this code to the returned value.

<?php
$julianDate
= gregoriantojd($month, $day, $year);

//correct for half-day offset
$dayfrac = date('G') / 24 - .5;
if (
$dayfrac < 0) $dayfrac += 1;

//now set the fraction of a day
$frac = $dayfrac + (date('i') + date('s') / 60) / 60 / 24;

$julianDate = $julianDate + $frac;
?>
up
0
httpwebwitch
19 years ago
This function also ignores decimal fractions in JD dates, and it uses non-standard format for returning the Gregorian date.

So, if your JD date is 2453056.28673, the Gregorian returned value is 2/20/2004, not "2004-02-20 23:45:36"

The decimal part is important, since the Julian day begins at noon, for example 2453056.49 is on Friday, 2453056.50 is on Saturday. Discarding the decimal part means that your returned Gregorian Date will be wrong 50% of the time.
up
-5
jfg
14 years ago
If you need the same output as the g_date_get_julian function of the GlibC, here is my php implementation :

<?php
/**
* Glib g_date_get_julian PHP implementation
*
* @param $str Date string in a format accepted by strtotime
* @author jfg
*/
private function _get_julian( $str )
{
$d = date_create($str);

if(
$d == false )
return
0;

$day_in_year = (int) date_format($d, "z");
$year = (int) date_format($d, "Y") - 1;
$julian_days = $year * 365;
$julian_days += ($year >>= 2);
$julian_days -= ($year /= 25);
$julian_days += $year >> 2;
$julian_days += $day_in_year + 1;

return
ceil($julian_days);
}

?>
To Top