note 42786 deleted from ref.calendar by tomsommer
| From: | tomsommer@php.net | Date: | Sat, 29 May 2004 20:41:28 +0000 |
| Subject: | note 42786 deleted from ref.calendar by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-70285@lists.php.net to get a copy of this message | ||
Note Submitter: HW
----
I needed to store times in Julian Day format as well as dates (for an astronomical application). In
line with astronomical conventions, times are represented as fractions of a day (with respect to
UTC/GMT).
The code could be simplified a lot by using the Calendar functions, but I couldn't rely on them
being available.
<?php
define('JULIAN_MINYEAR',-4713);
define('JULIAN_MINMONTH', 11);
define('JULIAN_MINDAY', 23);
// Arguments in the same order as for mktime()
function date2jd($h=null,$i=null,$s=null,$m=null,$d=null,$y=null)
{
if(func_num_args()<6) // Some defaults are used
{
$now = explode('|',gmdate('G|i|s|n|d|Y'));
switch(func_num_args())
{
//With fallthroughs.
case 0: $h = $now[0];
case 1: $y = $now[1];
case 2: $s = $now[2];
case 3: $m = $now[3];
case 4: $d = $now[4];
case 5: $y = $now[5];
}
}
$m12 = intval(($m-14)/12);
$jd = ((($y+4800+$m12)*1461)>>2)
+intval(367*($m-2-12*$m12)/12)
-((3*intval(($y+4900+$m12)/100))>>2)
+$d-32075;
$jd += (($s/60+$i)/60+$h)/24-.5;
return $jd;
}
function jd2date($jd)
{
$jdi = intval(floor($jd));
$jdf = $jd-$jdi;
$l = $jdi + 68569;
$n = intval((4 * $l) / 146097);
$l -= (146097 * $n + 3) >> 2;
$i = intval((4000 * ($l + 1)) / 1461001);
$l += 31 - ((1461 * $i) >> 2);
$j = intval((80 * $l) / 2447);
$d = 1 + $l - intval((2447 * $j) / 80);
$l = intval($j / 11);
$m = ($j + 2) - (12 * $l);
$y = 100 * ($n - 49) + $i + $l;
$jdf *= 24;
$h = intval($jdf);
$jdf -= $h;
$h -= 12;
$jdf *= 60;
$i = intval($jdf);
$jdf -= $i;
$jdf *= 60;
$s = intval($jdf);
return array($y,$m,$d,$h,$i,$s);
}
function time2jd($timestamp)
{
list($y,$m,$d,$h,$i,$s) = explode('|',gmdate('Y|n|j|G|i|s', $timestamp));
return date2j($h,$i,$s,$m,$d,$y);
}
function jd2time($jd)
{
list($y,$m,$d,$h,$i,$s) = j2date($jd);
return mktime($h,$i,$s,$m,$d,$y);
}
function timestampToJD($now=null)
{
if($now===null) $now=time();
list($y,$m,$d,$h,$i,$s) = explode('|',gmdate('Y|n|j|G|i|s', $now));
return date2jd($h,$i,$s,$m,$d,$y);
}
?>