Temperature Conversions Script

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

1. pein87New Member

Joined:
Aug 6, 2010
Messages:
173
47
Trophy Points:
0
Occupation:
Web Dev
Location:
Limbo
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 = 5 * (\$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 * 9 / 5 + 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 =  9 * (\$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 =  5 * (\$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 = 5 * (\$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) / 5 + 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;        }    }}?> ```

2. pankaj.seaNew Member

Joined:
Apr 6, 2009
Messages:
461
13
Trophy Points:
0
Occupation:
Web Developer
Location:
Kolkata

3. shrikrishnatechNew Member

Joined:
Nov 19, 2010
Messages:
42
4
Trophy Points:
0
Occupation:
Wordpress theme, Wordpress Theming, WP Themes, Cor

4. sneha123New Member

Joined:
Dec 8, 2010
Messages:
17
0
Trophy Points:
0
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_AryanBanned

Joined:
Nov 16, 2010
Messages:
40
2
Trophy Points:
0
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.
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>