CoderZone.org

Category: >> PHP Code >> Convert miles to feet, feet to miles, or inches. Bookmark and Share

<< lastnext >>

Snippet Name: Convert miles to feet, feet to miles, or inches.

Description: Handy function to convert miles to feet, feet to miles, miles to inches, etc.

Also see:
» Convert Minutes to Days, Hours, an...
» Resize and convert an image to a P...
» Convert text to image in php
» Convert Dollars And Cents Value to...
» LDAP Timestamp Converter
» BR2NL Function - Opposite of NL2BR
» Convert Seconds to Hours:Minutes:S...
» Convert UK Dates To mySQL Format D...
» Roman2dec and Dec2Roman
» Convert Minutes to Hours
» Binary to Text / Text to Binary
» Decimal to octal conversion
» Convert minutes to hours #2
» Convert minutes to hours #1
» Convert HTML to plain text
» Convert BBCode Tags
» Secure Auto-Link Maker
» Auto Link Code

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

 
<?php
// position 1 = distance numeric numbers only no commas
// position 2 = current length type of position 1
// position 3 = convert length type of position 1 and 2
// position 4 = FT,MI,IN extentions on or off :: 1=on : 2=off
// m = miles
// f = feet
// i = inches
 
function distance($curlen,$type,$totype,$on){
 
    // Check to see if the first value is an integer
    if(!is_int($curlen)){
        return 'Wrong input type for first value';
        exit;
    }
 
    // Make sure second value is either f,i, or m
    if($type!="f"&&$type!="i"&&$type!="m"){
        return 'Wrong input type for second value';
        exit;
    }
 
    // Make sure third value is either f,i, or m
    if($totype!="f"&&$totype!="i"&&$totype!="m"){
        return 'Wrong input type for third value';
        exit;
    }
 
    // Make sure fourth value is either 1 or 2
    if($on!=1&&$on!=2){
        return 'Wrong input type for fourth value';
        exit;
    }
 
    // If it validate the tests above, get the current type
    switch($type){
        // if it is an i do the math
        case "i":
            if($totype=="f"){
                $len = $curlen / 12;
                if($on==1){
                    return number_format(round($len,1)).' FT';
                }else{
                    return number_format(round($len,1));
                }
            }elseif($totype=="m"){
                $len = $curlen / 63360;
                if($on==1){
                    return number_format(round($len,1)).' MI';
                }else{
                    return number_format(round($len,1));
                }
            }
        break;
        // if it is an f do the math
        case "f":
            if($totype=="i"){
                $len = $curlen * 12;
                if($on==1){
                    return number_format(round($len,1)).' IN';
                }else{
                    return number_format(round($len,1));
                }
            }elseif($totype=="m"){
                $len = $curlen / 5280;
                if($on==1){
                    return number_format(round($len,1)).' MI';
                }else{
                    return number_format(round($len,1));
                }
            }
        break;
        // if it is an m do the math
        case "m":
            if($totype=="i"){
                $len = $curlen * 63360;
                if($on==1){
                    return number_format(round($len,1)).' IN';
                }else{
                    return number_format(round($len,1));
                }
            }elseif($totype=="f"){
                $len = $curlen * 5280;
                if($on==1){
                    return number_format(round($len,1)).' FT';
                }else{
                    return number_format(round($len,1));
                }
            }
        break;
    }
}
 
 
echo distance(123,"m","f",1);
 
?> 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 5