0

Here's an PHP example to go from Julian date to Gregorian date:

$jd = gregoriantojd(10,3,1975);
echo($jd . "<br />");

$gregorian = jdtogregorian($jd);
echo($gregorian);

Output:
2442689
10/3/1975

This works if you have the full Julian date, but what if you only have a Julian day like '254' and the year '2012'? How do you get to a Gregorian date with just the Julian day and Gregorian year with PHP?

Here's a graph of Julian days:

http://landweb.nascom.nasa.gov/browse/calendar.html

Is this possible with PHP?

Edit: Based on the answers, here's what I came up with, although there may be an easier way:

$JulianDay = 77;
$Year = 1977;
$DateYear = date("Y/m/d", mktime(0, 0, 0, 1, 1, $Year));
$GregDate = new DateTime($DateYear);
$GregDate->modify("+$JulianDay day");
$Date = $GregDate->format('Y/m/d');
echo $Date; // '1977/03/19'
user1636317
  • 3
  • 1
  • 3

3 Answers3

1
echo jdtogregorian(gregoriantojd(01, 01, 2012)+253);

If 254 in 2012 is September 10 (it seems like it from your link) it works fine.

aswyx
  • 345
  • 1
  • 5
  • 14
  • This was off by one day for some reason... couldn't figure out why though. Thanks though this helped me get unstuck. – user1636317 Aug 30 '12 at 20:30
  • My code generate the julian date for the first of january in the given year. Since the first of january is always 1 in julian, you have to subtract 1 from your julian day before the addition to get the correct result. I'm glad I could help; you can accept my answer to show your gratitude ;) – aswyx Aug 31 '12 at 07:01
0
$date = strtotime('+'.YourDay.' days', mktime(0, 0, 0, 0, 0, YourYear));

strtotime is and incredible function

bokan
  • 3,601
  • 2
  • 23
  • 38
-1

You can use below function in php to do the same.

function d2julian($indate) {
    $year = ltrim(date('y', $indate),'0');     /* Year part with leading zeroes stripped    */
    if ($year == 0) $outdate = '00';
    else if ($year < 10) $outdate = '0' . $year;
    else $outdate = $year;

    $day = ltrim(date('z',$indate)+1,'0');    /* Day with leading zeroes stripped            */
    if ($day < 10) $outdate .= '00' . $day;
    else if ($day < 100) $outdate .= '0' . $day;
    else $outdate .= $day;

    return $outdate;
}

Hope this helps. Let me know in case you have any questions.

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54