Here is the page code for my BASIC-to-php implementation of the algorithms to calculate the (geocentric) RA & Dec of the Sun for a given date from Jean Meeus' book Astronomical Algorithms as well as calculating the angular distance between two celestial objects. I can't remember the accuracy of the calculations and I didn't make the corrections for observer lat & long.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1" />
<base href="sionnagh.com" />
<title>Mick Todd Distance Calculator</title>
<STYLE TYPE="text/css">
<!--
BODY
{
font-family:sans-serif;
}
A:link{color:white}
A:visited{color:yellow}
-->
</STYLE>
</head>
<body>
<?php include("mtheader.php"); ?>
<h1>Mick Todd Distance Calculator</h1>
<?php
$earthincl=23.44; //Earth's axial tilt
$sollat=0;
$fdate = $_POST["fdate"];
if ($fdate=="") {
$fdate=getdate(date("U")-date("Z"));
$fday=$fdate[mday];
if (strlen($fday)==1) {$fday="0".$fday;}
$fmonth=$fdate[mon];
if (strlen($fmonth)==1) {$fmonth="0".$fmonth;}
$fyear=$fdate[year];
//$fdate=$fdate[mday]."/".$fdate[mon]."/".$fdate[year];
$fdate=$fday."/".$fmonth."/".$fyear;
}
else {
$fday=substr($fdate,0,2);
$fmonth=substr($fdate,3,2);
$fyear=substr($fdate,6,4);
}
$fra1 = $_POST["fra1"];
$fra1 = strtolower($fra1);
if ($fra1<>"") {
if (strpos($fra1,"h")==0) {
$frad1=$fra1;
$fra1=converttohms($fra1);
}
else {
$frad1=converthtod($fra1);
}
}
$fdec1 = $_POST["fdec1"];
$fdec1 = strtolower($fdec1);
if ($fdec1<>"") {
if (strpos($fdec1,"d")==0) {
$fdecd1=$fdec1;
$fdec1=converttodms($fdec1);
}
else {
$fdecd1=convertdtod($fdec1);
}
}
$fra2 = $_POST["fra2"];
$fra2 = strtolower($fra2);
if ($fra2<>"") {
if (strpos($fra2,"h")==0) {
$frad2=$fra2;
$fra2=converttohms($fra2);
}
else {
$frad2=converthtod($fra2);
}
}
$fdec2 = $_POST["fdec2"];
$fdec2 = strtolower($fdec2);
if ($fdec2<>"") {
if (strpos($fdec2,"d")==0) {
$fdecd2=$fdec2;
$fdec2=converttodms($fdec2);
}
else {
$fdecd2=convertdtod($fdec2);
}
}
function JulianDay ($fday, $fmonth, $fyear, $fUT) {
$JDpartsign=(100*$fyear+$fmonth-190002.5)/abs(100*$fyear+$fmonth-190002.5);
$JD = 367*$fyear - floor((7*($fyear+floor(($fmonth+9)/12)))/4) + floor((275*$fmonth)/9) + $fday + 1721013.5 + $fUT/24 - 0.5*$JDpartsign + 0.5;
return $JD;
}
function converttodms($inval) {
if ($inval==0) {
$fdsign=1;
}
else {
$fdsign=$inval/abs($inval);
}
$inval=abs($inval);
$fddeg=floor($inval);
$remain=($inval-$fddeg)*60;
$fdmin=floor($remain);
if (strlen($fdmin)==1) { $fdmin="0".$fdmin; }
$remain=($remain-$fdmin)*60;
$fdsec=round($remain*100)/100;
if (strpos($fdsec,".")==1) { $fdsec="0".$fdsec; }
$fddeg=$fdsign*$fddeg;
$fdms=$fddeg."d".$fdmin."m".$fdsec."s";
return $fdms;
}
function converttohms($inval) {
$inval=$inval/15;
$fddeg=floor($inval);
$remain=($inval-$fddeg)*60;
$fdmin=floor($remain);
if (strlen($fdmin)==1) { $fdmin="0".$fdmin; }
$remain=($remain-$fdmin)*60;
$fdsec=round($remain*100)/100;
if (strpos($fdsec,".")==1) { $fdsec="0".$fdsec; }
$fdms=$fddeg."h".$fdmin."m".$fdsec."s";
return $fdms;
}
function converthtod($inval) {
$posh=strpos($inval,"h");
$posm=strpos($inval,"m");
$poss=strpos($inval,"s");
$temph=substr($inval,0,$posh);
if ($posm) {
$tempm=substr($inval,$posh+1,$posm-($posh+1));
}
if ($poss) {
$temps=substr($inval,$posm+1,$poss-($posm+1));
}
$fdms=($temph+$tempm/60+$temps/3600)*15;
return $fdms;
}
function convertdtod($inval) {
$posd=strpos($inval,"d");
$posm=strpos($inval,"m");
$poss=strpos($inval,"s");
$tempd=substr($inval,0,$posd);
if ($posm) {
$tempm=substr($inval,$posd+1,$posm-($posd+1));
}
if ($poss) {
$temps=substr($inval,$posm+1,$poss-($posm+1));
}
$fdms=$tempd+$tempm/60+$temps/3600;
return $fdms;
}
function SolarCoords($fday, $fmonth, $fyear, $fUT) {
//Julian Day of 1991/ 5/19 at 13 UT JD = 2448396.04167
//Julian day of 2000/01/01 at 12 UT JD = 2451545.0
//number of Julian days since 2000/01/01 at 12 UT -3148.95833
//number of Julian centuries since 2000/01/01 at 12 UT T = - 3148.95833/36525
//used by the algorithm for L = 0.086213780
$k = 2*pi()/360;
$T = (JulianDay($fday, $fmonth, $fyear, $fUT)-2451545.0)/36525;
//echo "\$T: ".$T."<br />";
//mean anomaly, degree
$M = 357.52910 + 35999.05030*$T - 0.0001559*$T*$T - 0.00000048*$T*$T*$T;
//echo "\$M: ".$M."<br />";
// mean longitude, degree
$L0 = 280.46645 + 36000.76983*$T + 0.0003032*$T*$T;
//echo "\$L0: ".$L0."<br />";
$DL = (1.914600 - 0.004817*$T - 0.000014*$T*$T)*sin($k*$M) + (0.019993 - 0.000101*$T)*sin($k*2*$M) + 0.000290*sin($k*3*$M);
//echo "\$DL: ".$DL."<br />";
// true longitude, degree
$L = $L0 + $DL;
if (abs($L)>360) {
$divsign=($L/360)/abs($L/360);
$div=floor(abs($L/360));
$L=$L-$divsign*$div*360;
if ($L<0) { $L=$L+360; }
}
$L=round($L*10000)/10000;
return $L;
}
?>
<script type="text/javascript">
<!--
function settoday() {
var d=new Date();
dday=d.getDate();
if (dday<10) { dday="0" + dday; }
dmonth=d.getMonth()+1;
if (dmonth<10) { dmonth="0" + dmonth; }
dyear=d.getFullYear();
ddate= dday + "/" + dmonth + "/" + dyear;
return ddate;
}
//-->
</script>
<form METHOD="POST" ACTION="mtdistcalc.php" name="mtdistcalc">
<p>Date <input type="text" name="fdate" id="fdate" size=10 value="<?php echo $fdate; ?>"> <input type="button" value="Now" onclick="fdate.value=settoday();"></p>
<p>Object coordinates (#1) RA <input type="text" name="fra1" id="fra1" size=15 value="<?php echo $fra1; ?>"> Decl <input type="text" name="fdec1" id="fdec1" size=15 value="<?php echo $fdec1; ?>"></p>
<p>Object coordinates (#2) RA <input type="text" name="fra2" id="fra2" size=15 value="<?php echo $fra2; ?>"> Decl <input type="text" name="fdec2" id="fdec2" size=15 value="<?php echo $fdec2; ?>"></p>
<p><input TYPE=SUBMIT VALUE="Calculate distances"></p>
</form>
<?php
echo "<table><tr><td>Date:</td><td>".$fday."/".$fmonth."/".$fyear." "."0000UT</td></tr>";
echo "<tr><td>Julian Day: </td><td>".JulianDay($fday,$fmonth,$fyear,"0")."</td></tr>";
echo "<tr><td>Ecliptic latitude of the Sun: </td><td>".$sollat."° (assumed)</td></tr>";
$sollong = SolarCoords($fday,$fmonth,$fyear,"0");
echo "<tr><td>Ecliptic longitude of the Sun: </td><td>".$sollong."° (".converttodms($sollong).")</td></tr>";
echo "<tr><td>Earth's axial inclination: </td><td>".$earthincl."°</td></tr></table><br />";
//echo "\$feclong ".$feclong."<br /><br />";
$deltaSol = round(asin(sin($sollat*M_PI/180)*cos($earthincl*M_PI/180) + cos($sollat*M_PI/180)*sin($earthincl*M_PI/180)*sin($sollong*M_PI/180))*180/M_PI*10000)/10000;
$alphaSol = round(acos(cos($sollong*M_PI/180)*cos($sollat*M_PI/180) / cos($deltaSol*M_PI/180))*180/M_PI*10000)/10000;
if ($sollong>180) {
$alphaSol = 360-$alphaSol;
}
echo "<table><tr><td colspan=2>Sol</td></tr><tr><td>RA: </td><td>".$alphaSol."° (".converttohms($alphaSol).")</td></tr><tr><td>Dec:</td><td>".$deltaSol."° (".converttodms($deltaSol).")</td></tr></table><br />";
//spherical angular distance
//cos(theta) = sin(delta_a) sin(delta_b) + cos(delta_a) cos(delta_b) cos(alpha_a-alpha_b)
if ($fra1<>"" AND $fdec1<>"") {
$theta1 = round(acos(sin($deltaSol*M_PI/180)*sin($fdecd1*M_PI/180) + cos($deltaSol*M_PI/180)*cos($fdecd1*M_PI/180)*cos(($alphaSol-$frad1)*M_PI/180))*180/M_PI*1000)/1000;
}
if ($fra2<>"" AND $fdec2<>"") {
$theta2 = round(acos(sin($deltaSol*M_PI/180)*sin($fdecd2*M_PI/180) + cos($deltaSol*M_PI/180)*cos($fdecd2*M_PI/180)*cos(($alphaSol-$frad2)*M_PI/180))*180/M_PI*1000)/1000;
}
if ($fra1<>"" AND $fdec1<>"" AND $fra2<>"" AND $fdec2<>"") {
$theta3 = round(acos(sin($fdecd1*M_PI/180)*sin($fdecd2*M_PI/180) + cos($fdecd1*M_PI/180)*cos($fdecd2*M_PI/180)*cos(($frad1-$frad2)*M_PI/180))*180/M_PI*1000)/1000;
echo "<p><font color=\"#0000FF\">Angular distance between Object #1 and Object #2 is: ".$theta3."°</font></p>";
}
if (($fra1<>"" and $fdec1<>"") or ($fra2<>"" and $fdec2<>"")) {
echo "<table><tr><td> </td><td align=center><b>RA</b></td><td align=center><b>Dec</b></td><td><b>Solar elong.</b></td></tr>";
}
if ($fra1<>"" AND $fdec1<>"") {
echo "<tr><td>Object #1:</td><td><font color=Maroon>".$fra1."</font></td><td><font color=Maroon>".$fdec1."</font></td><td> <font color=Maroon>".$theta1."°</font></td></tr>";
}
if ($fra2<>"" AND $fdec2<>"") {
echo "<tr><td>Object #2:</td><td>".$fra2."</td><td>".$fdec2."</td><td> ".$theta2."°</td></tr>";
}
?>
</body>
</html>
12-lon/15
hours (longitude is negative west of Greenwich). This formula is for the fictitious "mean sun" and is accurate to within 15 minutes. To correct for those extra 15 minutes, see Equation of Time. $\endgroup$