1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Temperature Conversions Script

Discussion in 'PHP' started by pein87, Dec 29, 2010.

  1. pein87

    pein87 New Member

    This is temperature conversion script, used to convert temperature from Celsius, Fahrenheit, Rankine, and Kelvin.

    The Code



    Include the script in your php project and call one of the conversion functions with a numeric value as its parameter to get some output. You can convert from Celsius, Fahrenheit, Rankine, and Kelvin temperature types. The functions all except 2 parameters the value(int), and round(true). Because if round does not equal true it will not round but if it does equal true it will round the value using the built in round function.

    I've tested the math invloved to make sure the out puts are correct. I did not use the float values because I would have to include ceil and floor options which I felt weren't needed but if you check out this wiki page you can try your hand at the conversions there(I used them as my source) http://en.wikipedia.org/wiki/Rankine_scale

    PHP:
    <?php

    $NaN 
    "NaN";

    /* Celsius to and from Fahrenheit conversions */

    function fahrenheitToCelsius($far,$round="true")
    {

        global 
    $NaN;    
        if(!
    is_numeric($far))
        {
            return 
    $NaN;
        }
        else
        {
            
    $CelsiusVal * ($far 32) / 9;    
            if(
    $round == "true")
            {
                return 
    round($CelsiusVal);    
            }
            else
            {
                return 
    $CelsiusVal;
            }
        }
    }

    function 
    celsiusToFahrenheit($cel$round="true")
    {
        global 
    $NaN;
        if(!
    is_numeric($cel))
        {
            return 
    $NaN;
        }
        else
        {
            
    $FahrenheitVal $cel 32;    
            if(
    $round == "true")
            {
                return 
    round($FahrenheitVal);
            }
            else
            {
                return 
    $FahrenheitVal;
            }
        }
    }

    /* Kelvin to and from conversions */

    function celsiusToKelvin($cel$round="true")
    {
        global 
    $NaN;
        if(!
    is_numeric($cel))
        {
            return 
    $NaN;
        }
        else
        {
            
    $KelvinVal $cel 273;
            if(
    $round == "true")
            {
                return 
    round($KelvinVal);
            }
            else
            {
                return 
    $KelvinVal;
            }
        }
    }

    function 
    kelvinToCelsius($kel$round="true")
    {
        global 
    $NaN;    
        if (!
    is_numeric($kel))
        {
            return 
    $NaN;
        }
        else
        {
            
    $CelsiusVal $kel 273;    
            if (
    $round == "true")
            {
                return 
    round($CelsiusVal);
            }
            else
            {
                return 
    $CelsiusVal;
            }
        }
    }

    function 
    kelvinToFahrenheit($kel$round="true")
    {
        global 
    $NaN;    
        if(!
    is_numeric($kel))
        {
            return 
    $NaN;
        }
        else
        {
            
    $FahrenheitVal =  * ($kel 459) / 5;    
            if(
    $round == "true")
            {
                return 
    round($FahrenheitVal);
            }
            else
            {
                return 
    $FahrenheitVal;
            }
        }
    }

    function 
    fahrenheitToKelvin($far$round="true")
    {
        global 
    $NaN;
        if(!
    is_numeric($far))
        {
            return 
    $NaN;
        }
        else
        {
            
    $KelvinVal =  * ($far 459) / 9;
            if(
    $round == "true")
            {
                return 
    round($KelvinVal);
            }
            else
            {
                return 
    $KelvinVal;
            }
        }
    }

    function 
    kelvinToRankine($kel$round="true")
    {
        global 
    $NaN;
        if (!
    is_numeric($kel))
        {
            return 
    $NaN;
        }
        else
        {
            
    $RankineVal = ($kel 9) / 5;
            if(
    $round == "true")
            {
                return 
    round($RankineVal);
            }
            else
            {
                return 
    $RankineVal;
            }
        }
    }

    function 
    rankineToKelvin($ran$round="true")
    {
        global 
    $NaN;
        if(!
    is_numeric($ran))
        {
            return 
    $NaN;
        }
        else
        {
            
    $KelvinVal = ($ran 5) / 9;
            if(
    $round == "true")
            {
                return 
    round($KelvinVal);
            }
            else
            {
                return 
    $KelvinVal;
            }
        }
    }

    /* To and From Rankine using Celsius and Fahrenheit because Rankine to Kelvin is defined in Kelvin functions  */

    function rankineToCelsius($ran$round="true")
    {
        global 
    $NaN;    
        if(!
    is_numeric($ran))
        {
            return 
    $NaN;
        }
        else
        {
            
    $CelsiusVal * ($ran 491) / 9;    
            if(
    $round == "true")
            {
                return 
    round($CelsiusVal);
            }
            else
            {
                return 
    $CelsiusVal;
            }
        }
    }

    function 
    celsiusToRankine($cel$round="true")
    {

        global 
    $NaN;
        
        if(!
    is_numeric($cel))
        {
            return 
    $NaN;
        }
        else
        {
            
    $RankineVal = ($cel 9) / 491;    
            if(
    $round == "true")
            {
                return 
    round($RankineVal);
            }
            else
            {
                return 
    $RankineVal;
            }
        }
    }

    function 
    fahrenheitToRankine($far$round="true")
    {
        global 
    $NaN;
        if(!
    is_numeric($far))
        {
            return 
    $NaN;
        }
        else
        {
            
    $RankineVal $far 459;
            if(
    $round == "true")
            {
                return 
    round($RankineVal);
            }
            else
            {
                return 
    $RankineVal;
            }
        }
    }

    function 
    rankineToFahrenheit($ran$round="true")
    {
        global 
    $NaN;    
        if(!
    is_numeric($ran))
        {
            return 
    $NaN;
        }
        else
        {
            
    $FahrenheitVal $ran 459;
            if(
    $round == "true")
            {
            
                return 
    round($FahrenheitVal);
            }
            else
            {
                return 
    $FahrenheitVal;
            }
        }
    }
    ?>

    References



     
  2. pankaj.sea

    pankaj.sea New Member

    Good Info added...
    :)
     
  3. shrikrishnatech

    shrikrishnatech New Member

    nice script its really helpful
     
  4. sneha123

    sneha123 New Member

    Hey ,Nice thread,,,,,this is very informative site,,,,,,thanks for this information will really help us..... thanks for sharing with us…its great post! thanks a lot for these guides..they're really helpful.,,,,,,,Well done !!!!!!!!
     
  5. Seo_Aryan

    Seo_Aryan Banned

    1.
    <script type="text/javascript">
    2.
    var fahrenheit= Number(document.temp_conv.fahr.value);
    3.
    var celsius= Number(document.temp_conv.cels.value);
    4.
    var kelvin= Number(document.temp_conv.kelv.value);
    5.

    6.
    if(isFinite(fahrenheit)){
    7.

    8.
    function conversion() {
    9.
    var f= Number(document.temp_conv.fahr.value);
    10.
    var c= Number(document.temp_conv.cels.value);
    11.
    var k= Number(document.temp_conv.kelv.value);
    12.
    var f1= 5/9;
    13.
    var s1= f-32;
    14.

    15.
    var cels= f1*s1
    16.
    document.temp_conv.cels.value = cels;
    17.

    18.
    var kelv= cels + 273
    19.
    document.temp_conv.kelv.value = kelv;
    20.
    }
    21.
    }
    22.

    23.
    if(isFinite(celsius)){
    24.

    25.
    function conversion() {
    26.
    var f= Number(document.temp_conv.fahr.value);
    27.
    var c= Number(document.temp_conv.cels.value);
    28.
    var k= Number(document.temp_conv.kelv.value);
    29.

    30.
    var fahr= 1.8*c+32
    31.
    document.temp_conv.fahr.value = fahr;
    32.

    33.
    var kelv= c + 273
    34.
    document.temp_conv.kelv.value = kelv;
    35.
    }
    36.
    }
    37.

    38.
    if(isFinite(kelvin)){
    39.

    40.
    function conversion() {
    41.
    var f= Number(document.temp_conv.fahr.value);
    42.
    var c= Number(document.temp_conv.cels.value);
    43.
    var k= Number(document.temp_conv.kelv.value);
    44.

    45.
    var cels= k - 273
    46.
    document.temp_conv.kelv.value = kelv;
    47.

    48.
    var fahr= 1.8*cels+32
    49.
    document.temp_conv.fahr.value = fahr;
    50.
    }
    51.
    }
    52.
    </script>
    53.

    54.
    </head>
    55.
    <body>
    56.

    57.
    <form name="temp_conv">
    58.
    Fahrenheit<br />
    59.
    <input type="text" name="fahr" id="fahr" value="" /><br />
    60.
    Celsius<br />
    61.
    <input type="text" name="cels" id="cels" value="" /><br />
    62.
    Kelvin<br />
    63.
    <input type="text" name="kelv" id="kelv" value="" />
    64.
    <input type="button" value="Convert" onclick="conversion()" />
    65.
    </form>
     

Share This Page