Converting Degree, Minutes, Seconds (DMS) to decimal in PHP

Azlan Nohara picture Azlan Nohara · Mar 11, 2014 · Viewed 18.2k times · Source

Currently, I'm learning to use Google Maps API. From what I read, the API require the latitude and longitude in Decimal Degree (DD).

In my database, the data is stored as DMS.

Example, 110° 29' 01.1"

I would to ask if you guys have any DMS to DD in php. And, the converter must accept from a single string like the example above.

Regards

Answer

Romi picture Romi · Mar 11, 2014

You can try if this is working for you.

<?php

function DMStoDD($deg,$min,$sec)
{

    // Converting DMS ( Degrees / minutes / seconds ) to decimal format
    return $deg+((($min*60)+($sec))/3600);
}    

function DDtoDMS($dec)
{
    // Converts decimal format to DMS ( Degrees / minutes / seconds ) 
    $vars = explode(".",$dec);
    $deg = $vars[0];
    $tempma = "0.".$vars[1];

    $tempma = $tempma * 3600;
    $min = floor($tempma / 60);
    $sec = $tempma - ($min*60);

    return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
}    

?>