Temperature Converter

A simple temperature converter that allows you to convert from any temperature scale to every other, as long as the scales follows the equation Celsius = (scale * X) Y. New scales can also easily be added if you find X and Y in that equation.

<?php
/*
	________________________________________________________________________
	|									|
	|	TEMPERATURE CONVERTER						|
	|									|
	|	Developer:	Nathan Lex					|
	|	Published:	Saturday, June 20, 2009				|
	|	Last Mod:	Sunday, June 21, 2009				|
	|	Version:	1.1.0						|
	|	Categories:	Science, Converters				|
	|	Languages:	PHP, HTML					|
	|	License:	GNU General Public License v3			|
	|	ICANN Web:	http://whitehat.servehttp.com/tools/ 		|
	|									|
	|	This utility allows the user to convert between most		|
	|	scales of temperature. This script first converts the		|
	|	input temperature to Celcius, then converts it to every		|
	|	other scale. New scales of measure can be easily added 		|
	|	if it follows the formula:					|
	|		Celcuis = (newscale + X) * Y				|
	|_______________________________________________________________________|

 */

	$d = "°";
	$scales = array(
		//°C = (K + -273.15) * 1
		"Kelvin" => array(
			"unit" => "K",
			"add"  => -273.15,
			"mult" => 1,
		),
		//°C = (K + 0) * 1
		"Celcius" => array(
			"unit" => $d."C",
			"add"  => 0,
			"mult" => 1,
		),
		//°C = (°F + -32) * 5/9
		"Fahrenheit" => array(
			"unit" => $d."F",
			"add"  => -32,
			"mult" => (5/9),
		),
		//°C = (R + -491.67) * 5/9
		"Rankine" => array(
			"unit" => "R",
			"add"  => -491.67,
			"mult" => (5/9),
		),
		//°C = (°N + -491.67) * 100/33
		"Newton" => array(
			"unit" => $d."N",
			"add"  => -491.67,
			"mult" => (100/33),
		),
		//°C = (°Ré + 0) * 5/4
		"Réaumur" => array(
			"unit" => $d."Ré",
			"add"  => 0,
			"mult" => (5/4),
		),
		//°C = (°Rø + -7.5) * 40/21
		"Rømer" => array(
			"unit" => $d."Rø",
			"add"  => -7.5,
			"mult" => (40/21),
		),
	);
	
	function convert($fromscale, $fromvalue, $toscale) {
		if ($fromvalue != "") {
			global $scales;
			$Celcius = ($fromvalue + $scales[$fromscale]["add"]) * $scales[$fromscale]["mult"];
			$output = ($Celcius * (1 / $scales[$toscale]["mult"])) - $scales[$toscale]["add"];
			return $output;
		}
	}
	
	foreach ($scales as $name => $data) {
		if ($_POST[$name] != "") {
			$inputvalue = $_POST[$name];
			$inputscale = $name;
		}
	}
	
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
		<TITLE>Temperature Converter</TITLE>
	</HEAD>
	<BODY LANG="en-US" DIR="LTR">
		<TABLE HEIGHT=100% WIDTH=100%>
			<COL WIDTH=100%>
			<TR>
				<TD COLSPAN=1 WIDTH=100% VALIGN=CENTER>
					<CENTER>
						<TABLE BORDER=1 CELLPADDING=4 CELLSPACING=3>
							<TR>
								<TD>
									<CENTER>
										<H3>Temperature Converter</H3>
									</CENTER>
									<HR/>
									<TABLE>
										<TR>

<!-- Begin php script -->
<?php
	foreach ($scales as $name => $data) {
		echo "<TD VALIGN=\"CENTER\">\n";
		echo "\t<FORM METHOD=\"POST\" ACTION=\"\" NAME=\"$name\">\n";
		echo "\t\t<CENTER>\n";
		echo "\t\t\t" . $name . " (" . $data["unit"] . ")\n";
		echo "\t\t</CENTER>\n";
		echo "\t\t<CENTER>\n";
		echo "\t\t\t<INPUT TYPE=\"TEXT\" NAME=\"$name\" SIZE=\"16\" VALUE=\"" . convert($inputscale, $inputvalue, $name) . "\">\n";
		echo "\t\t</CENTER>\n";
		echo "\t\t<CENTER>\n";
		echo "\t\t\t<INPUT TYPE=\"SUBMIT\" VALUE=\"Convert\"/>\n";
		echo "\t\t</CENTER>\n";
		echo "\t</FORM>\n";
		echo "</TD>\n";
	}
?>
<!-- End php script -->
										</TR>
									</TABLE>
									<HR/>
									<CENTER>
										<TABLE CELLPADDING=4 >
											<TR>
												<TD>
													<A HREF="source">View source</A>
												</TD>
												<TD>
													<A HREF="../">More neat tools!</A>
												</TD>
											</TR>
										</TABLE>
									</CENTER>
								</TD>
							</TR>
						</TABLE>
					</CENTER>
				</TD>
			</TR>
		</TABLE>
	</BODY>

</HTML>

Usage

Copy and paste entire code above. A simple web page is already included.

EDIT: WHY THE F*** WON'T IT SHOW MY CODE! NO MATTER HOW MANY TIMES I PASTE IT!

Well, you can find the page at http://whitehat.servehttp.com/tools/temperature/
I just copy and pasted the code for that page then removed the bottom part that says "View Source More Neat Tools!" and put the result into my snap.

The source code can be achieved by clicking the "View Source" Link near the bottom of the page.


Comments

abhilash
Posted on 19.09.2009 10:12

goood

Add your comment