CakeFest 2024: The Official CakePHP Conference

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 年、ロシア(USSR)は 1918 年、ギリシャは 1923 年に 移行しました。 ほとんどのヨーロッパの国々では、グレゴリウス暦の前は ユリウス暦を使用していました。

パラメータ

month

月を表す、1 (January) から 12 (December) までの数字。 月の最後の日が与えられた日より少ない場合、オーバーフローが発生します; 例を参照ください。

day

日を表す、1 から 31 までの数字。

year

年を表す、-4714 から 9999 までの数字。 負の値は B.C を意味し、正の値は A.D を意味しています。 0 という年はないことに注意してください。 つまり、B.C 元年の12月31日の次は、A.D 元年の1月1日です。

戻り値

指定したグレゴリウス日をユリウス積算日になおした結果を返します。 範囲外の日付については 0 を返します。

例1 カレンダー関数

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

上の例の出力は以下となります。

2440871
10/11/1970

例2 Overflow behavior

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

上の例の出力は以下となります。

2458181
2458181

参考

  • jdtogregorian() - ユリウス積算日をグレゴリウス日に変換する
  • cal_to_jd() - サポートされるカレンダーからユリウス積算日に変換する

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