# [PHP-DEV] Bug #9076 Updated: GregorianToJD function returns incorrect values

```ID: 9076
Updated by: jmoore
Reported By: [EMAIL PROTECTED]
Old-Status: Open
Status: Assigned
Bug Type: Calendar related
PHP Version: 4.0.4pl1
Assigned To: jmoore

Ill look at these patches at some point soon.

- James

---------------------------------------------------------------------------

[2001-02-09 07:15:34] [EMAIL PROTECTED]
I found more thoroughly-researched algorithms for
calculating to and from Gregorian/Julian calender dates
to Julian Day Count values:

http://www.capecod.net/~pbaum/date/date0.htm

I suggest that the PHP Calender functions be based
upon the algorithms here, as meticulous care seems
to have been taken to formulate and proof them.

In the meantime, I have written my own
implementations of the GregorianToJD and
JDToGregorian functions in PHP. I have tested them
using the examples in Table 2 of Chapter 1 of Baum's
work and they pass.

function php_gregoriantojd(\$input_year, \$input_month,
\$input_day) {
// Make sure supplied arguments are of the correct
form. Day may be a fractional value,
// but month and year must be integers.
\$input_year = intval(\$input_year);
\$input_month = intval(\$input_month);
\$input_day = doubleval(\$input_day);

// Adjust the start of the year so that it is in March.
if(\$input_month < 3) {
\$input_month += 12;
\$input_year -= 1;
}

// Calculate and return the Julian Day Count.
return \$input_day + floor((153 * \$input_month - 457) /
5) + 365 * \$input_year + floor(\$input_year / 4) -
floor(\$input_year / 100) + floor(\$input_year / 400) +
1721118.5;
}

function php_jdtogregorian(\$jdc) {
// Make sure that the Julian Day Count value is proper.
\$jdc = doubleval(\$jdc);

\$z = floor(\$jdc - 1721118.5);
\$r = \$jdc - 1721118.5 - \$z;
\$g = \$z - 0.25;
\$a = floor(\$g / 36524.25);
\$b = \$a - floor(\$a / 4);
\$gregorian["year"] = floor((\$b + \$g) / 365.25);
\$c = \$b + \$z - floor(365.25 * \$gregorian["year"]);
\$gregorian["month"] = floor((5 * \$c + 456) / 153);
\$gregorian["day"] = \$c - floor((153 *
\$gregorian["month"] - 457) / 5) + \$r;
if(\$gregorian["month"] > 12) {
\$gregorian["year"] += 1;
\$gregorian["month"] -= 12;
}

return \$gregorian;
}

The line breaks on all of that will probably be all
mangled when I post, but it should make sense when
straightened out...

---------------------------------------------------------------------------

[2001-02-02 11:05:26] [EMAIL PROTECTED]
The GregorianToJD Calender function returns incorrect
results. As far as I can tell, this applies to the
GregorianToSdn internal function in the PHP source.

There's not really any point putting a sample PHP script
here, as it's the algorithm that seems to be at fault. I
took two specific cases and worked through the
algorithm by hand (i.e. pen and paper). Here are the
results:

2nd February 2001 (2/2/2001) = 2451943
7th June 1980 (7/6/1980) = 2473618

As I understand it, the Julian Day Count is a uniform
count of days from some time around 4714 BC. Now,
how can 2/2/01 be a lesser number of days than
7/6/80?

I have no idea what the correct results for these test
cases are, nor any idea what particular part of the
algorithm is wrong. It's just wrong somewhere!

---------------------------------------------------------------------------

ATTENTION! Do NOT reply to this email!
To reply, use the web interface found at http://bugs.php.net/?id=9076&edit=2

--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

```