rhumb line calculation - javascript to php
<p>This example and java script code is from <a href="http://www.movable-type.co.uk/scripts/latlong.html">link text</a><br>
Look at the section on rhumb lines.</p>

<hr>

<p>Given a start point and a distance d along constant bearing θ, this will calculate the destination point. If you maintain a constant bearing along a rhumb line, you will gradually spiral in towards one of the poles. </p>

<p><strong>Formula</strong>:  </p>

<pre><code>  α = d/R (angular distance)   
  lat2 = lat1 + α.cos(θ)   
  Δφ = ln(tan(lat2/2+π/4)/tan(lat1/2+π/4)) [= the ‘stretched’ latitude difference] 
if E:W line q = cos(lat1)    
otherwise q = Δlat/Δφ   
  Δlon = α.sin(θ)/q   
  lon2 = (lon1+Δlon+π) % 2.π − π   
  where ln is natural log and % is modulo, Δlon is taking shortest route (&lt;180°), and R is the earth’s radius 
</code></pre>

<p><strong>JavaScript</strong>: </p>

<pre><code>lat2 = lat1 + d*Math.cos(brng);
var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
var q = (!isNaN(dLat/dPhi)) ? dLat/dPhi : Math.cos(lat1);  // E-W line gives dPhi=0
var dLon = d*Math.sin(brng)/q;
// check for some daft bugger going past the pole, normalise latitude if so
if (Math.abs(lat2) &gt; Math.PI/2) lat2 = lat2&gt;0 ? Math.PI-lat2 : -(Math.PI-lat2);
lon2 = (lon1+dLon+Math.PI)%(2*Math.PI) - Math.PI; 
</code></pre>

<hr>

<p>I am trying to convert it into php syntax but I am not getting the desired result. I have the latitude part working fine.  I also included my test data.</p>

<hr>

<h1>MY PHP CODE</h1>

<pre><code>// test data
$R = 6371;
$tlatitude = 50.7;
$tlongitude = -105.214;
$theading = 124;
$d = 50;  

$projlat = $tlatitude +  rad2deg(($d/$R)*COS(deg2rad($theading)));

//Δφ = ln(tan(lat2/2+π/4)/tan(lat1/2+π/4))
$delta_phi = log(tan(deg2rad($projlat/2) + pi()/4)/(tan(deg2rad($tlatitude/2) + pi()/4)));

//q = Δlat/Δφ 
$delta_lat = deg2rad($projlat - $tlatitude);

$q = $delta_lat/$delta_phi;


//Δlon = α.sin(θ)/q
$delta_long = rad2deg($d/$R*sin(deg2rad($theading))/$q);

$projlong = $tlongitude + $delta_long;
</code></pre>

<p>I get <code>$projlong = -104.84</code></p>

<p>according to the referenced page the answer should be <code>-104.63</code>. </p>

<p>Now I am trying to get this to work disregarding the east-west and over the pole possibilities.</p>
