Php: Convert Coordinates From Degrees to Decimal Format

PHP: Convert coordinates from degrees to decimal format
by Janeth Kent Date: 11-04-2013


This code will convert longitude and latitude in D'M'S format into a decimal format. It also contains the function to do the reverse.

The example below does contain a couple of strange lines of code. You will notice that we seperate the integer and floating point values from the decimal format using a set of string functions, instead of using the math round or floor functions. This fixes a 'feature' in some versions of PHP that causes it to behave in an unpredictable manner when handling math with floating points.
For example, if you include the code print(91.1 - 91); in PHP you may not get the result you expect. You will actually get 0.099999999999994, not 1 ! This tends to throw of the calculations in the example.

function DMStoDEC($deg,$min,$sec)

// Converts DMS ( Degrees / minutes / seconds )
// to decimal format longitude / latitude

return $deg+((($min*60)+($sec))/3600);


// Converts decimal longitude / latitude to DMS
// ( Degrees / minutes / seconds )

// This is the piece of code which may appear to
// be inefficient, but to avoid issues with floating
// point math we extract the integer part and the float
// part by using a string function.

$vars explode(".",$dec);
$deg $vars[0];
$tempma "0.".$vars[1];

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

    return array(



by Janeth Kent Date: 11-04-2013 hits : 3154  
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN END DEVELOPMENT.


We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration