Posted By

Scooter on 03/12/08


Tagged

conversion temperature unit


Versions (?)

Temperature Conversion


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com

This function library allows conversion between temperatures in the following scales: Celsius, Delisle, Fahrenheit, Kelvin, Newton, Rankine, Reaumur, and Romer.

  1. <?php
  2. /*
  3.   PHP Temperature Conversion Library
  4.  
  5.   Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  6.  
  7.   This work is licensed under the Creative Commons Attribution License. To view
  8.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  9.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  10.   94305, USA.
  11.  
  12.   This function library allows conversion between temperatures in the following scales:
  13.   Celsius, Delisle, Fahrenheit, Kelvin, Newton, Rankine, Reaumur, and Romer.
  14.  
  15.   Refer to Wikipedia for information on these temperature scales.
  16.   */
  17.  
  18. // Facade function, in case you'd rather not call the individual functions directly or you just like encapsulation.
  19. function convertTemperature($temperature, $originScale, $targetScale)
  20. {
  21. // Return the input value for temperature if both scales are identical.
  22. if (strtoupper($originScale) == strtoupper($targetScale))
  23. {
  24. return $temperature;
  25. }
  26. else
  27. {
  28. switch (strtoupper($originScale))
  29. {
  30. case "CELSIUS":
  31. switch (strtoupper($targetScale))
  32. {
  33. case "DELISLE":
  34. return CelsiusToDelisle($temperature);
  35. break;
  36. case "FAHRENHEIT":
  37. return CelsiusToFahrenheit($temperature);
  38. break;
  39. case "KELVIN":
  40. return CelsiusToKelvin($temperature);
  41. break;
  42. case "NEWTON":
  43. return CelsiusToNewton($temperature);
  44. break;
  45. case "RANKINE":
  46. return CelsiusToRankine($temperature);
  47. break;
  48. case "REAUMUR":
  49. return CelsiusToReaumur($temperature);
  50. break;
  51. case "ROMER":
  52. return CelsiusToRomer($temperature);
  53. break;
  54. }
  55. break;
  56. case "DELISLE":
  57. switch (strtoupper($targetScale))
  58. {
  59. case "CELSIUS":
  60. return DelisleToCelsius($temperature);
  61. break;
  62. case "FAHRENHEIT":
  63. return DelisleToFahrenheit($temperature);
  64. break;
  65. case "KELVIN":
  66. return DelisleToKelvin($temperature);
  67. break;
  68. case "NEWTON":
  69. return DelisleToNewton($temperature);
  70. break;
  71. case "RANKINE":
  72. return DelisleToRankine($temperature);
  73. break;
  74. case "REAUMUR":
  75. return DelisleToReaumur($temperature);
  76. break;
  77. case "ROMER":
  78. return DelisleToRomer($temperature);
  79. break;
  80. }
  81. break;
  82. case "FAHRENHEIT":
  83. switch (strtoupper($targetScale))
  84. {
  85. case "CELSIUS":
  86. return FahrenheitToCelsius($temperature);
  87. break;
  88. case "DELISLE":
  89. return FahrenheitToDelisle($temperature);
  90. break;
  91. case "KELVIN":
  92. return FahrenheitToKelvin($temperature);
  93. break;
  94. case "NEWTON":
  95. return FahrenheitToNewton($temperature);
  96. break;
  97. case "RANKINE":
  98. return FahrenheitToRankine($temperature);
  99. break;
  100. case "REAUMUR":
  101. return FahrenheitToReaumur($temperature);
  102. break;
  103. case "ROMER":
  104. return FahrenheitToRomer($temperature);
  105. break;
  106. }
  107. break;
  108. case "KELVIN":
  109. switch (strtoupper($targetScale))
  110. {
  111. case "CELSIUS":
  112. return KelvinToCelsius($temperature);
  113. break;
  114. case "DELISLE":
  115. return KelvinToDelisle($temperature);
  116. break;
  117. case "FAHRENHEIT":
  118. return KelvinToFahrenheit($temperature);
  119. break;
  120. case "NEWTON":
  121. return KelvinToNewton($temperature);
  122. break;
  123. case "RANKINE":
  124. return KelvinToRankine($temperature);
  125. break;
  126. case "REAUMUR":
  127. return KelvinToReaumur($temperature);
  128. break;
  129. case "ROMER":
  130. return KelvinToRomer($temperature);
  131. break;
  132. }
  133. case "NEWTON":
  134. switch (strtoupper($targetScale))
  135. {
  136. case "CELSIUS":
  137. return NewtonToCelsius($temperature);
  138. break;
  139. case "DELISLE":
  140. return NewtonToDelisle($temperature);
  141. break;
  142. case "FAHRENHEIT":
  143. return NewtonToFahrenheit($temperature);
  144. break;
  145. case "KELVIN":
  146. return NewtonToKelvin($temperature);
  147. break;
  148. case "RANKINE":
  149. return NewtonToRankine($temperature);
  150. break;
  151. case "REAUMUR":
  152. return NewtonToReaumur($temperature);
  153. break;
  154. case "ROMER":
  155. return NewtonToRomer($temperature);
  156. break;
  157. }
  158. break;
  159. case "RANKINE":
  160. switch (strtoupper($targetScale))
  161. {
  162. case "CELSIUS":
  163. return RankineToCelsius($temperature);
  164. break;
  165. case "DELISLE":
  166. return RankineToDelisle($temperature);
  167. break;
  168. case "FAHRENHEIT":
  169. return RankineToFahrenheit($temperature);
  170. break;
  171. case "KELVIN":
  172. return RankineToKelvin($temperature);
  173. break;
  174. case "NEWTON":
  175. return RankineToNewton($temperature);
  176. break;
  177. case "REAUMUR":
  178. return RankineToReaumur($temperature);
  179. break;
  180. case "ROMER":
  181. return RankineToRomer($temperature);
  182. break;
  183. }
  184. break;
  185. case "REAUMUR":
  186. switch (strtoupper($targetScale))
  187. {
  188. case "CELSIUS":
  189. return ReaumurToCelsius($temperature);
  190. break;
  191. case "DELISLE":
  192. return ReaumurToDelisle($temperature);
  193. break;
  194. case "FAHRENHEIT":
  195. return ReaumurToFahrenheit($temperature);
  196. break;
  197. case "KELVIN":
  198. return ReaumurToKelvin($temperature);
  199. break;
  200. case "NEWTON":
  201. return ReaumurToNewton($temperature);
  202. break;
  203. case "RANKINE":
  204. return ReaumurToRankine($temperature);
  205. break;
  206. case "ROMER":
  207. return ReaumurToRomer($temperature);
  208. break;
  209. }
  210. break;
  211. case "ROMER":
  212. switch (strtoupper($targetScale))
  213. {
  214. case "CELSIUS":
  215. return RomerToCelsius($temperature);
  216. break;
  217. case "DELISLE":
  218. return RomerToDelisle($temperature);
  219. break;
  220. case "FAHRENHEIT":
  221. return RomerToFahrenheit($temperature);
  222. break;
  223. case "KELVIN":
  224. return RomerToKelvin($temperature);
  225. break;
  226. case "NEWTON":
  227. return RomerToNewton($temperature);
  228. break;
  229. case "RANKINE":
  230. return RomerToRankine($temperature);
  231. break;
  232. case "REAUMUR":
  233. return RomerToReaumur($temperature);
  234. break;
  235. }
  236. break;
  237. }
  238. }
  239. }
  240.  
  241. // Converts a given temperature from Celsius to Delisle.
  242. function CelsiusToDelisle($temperature)
  243. {
  244. return (100 - $temperature) * 3 / 2;
  245. }
  246.  
  247. // Converts a given temperature from Celsius to Fahrenheit.
  248. function CelsiusToFahrenheit($temperature)
  249. {
  250. return ($temperature * 9 / 5) + 32;
  251. }
  252.  
  253. // Converts a given temperature from Celsius to Kelvin.
  254. function CelsiusToKelvin($temperature)
  255. {
  256. return $temperature + 273.15;
  257. }
  258.  
  259. // Converts a given temperature from Celsius to Newton.
  260. function CelsiusToNewton($temperature)
  261. {
  262. return $temperature * 33 / 100;
  263. }
  264.  
  265. // Converts a given temperature from Celsius to Rankine.
  266. function CelsiusToRankine($temperature)
  267. {
  268. return ($temperature + 273.15) * 1.8;
  269. }
  270.  
  271. // Converts a given temperature from Celsius to Reaumur.
  272. function CelsiusToReaumur($temperature)
  273. {
  274. return $temperature * 4 / 5;
  275. }
  276.  
  277. // Converts a given temperature from Celsius to Romer.
  278. function CelsiusToRomer($temperature)
  279. {
  280. return $temperature * 21 / 40 + 7.5;
  281. }
  282.  
  283. // Converts a given temperature from Delisle to Celsius.
  284. function DelisleToCelsius($temperature)
  285. {
  286. return 100 - $temperature * 2 / 3;
  287. }
  288.  
  289. // Converts a given temperature from Delisle to Fahrenheit.
  290. function DelisleToFahrenheit($temperature)
  291. {
  292. return 212 - $temperature * 6 / 5;
  293. }
  294.  
  295. // Converts a given temperature from Delisle to Kelvin.
  296. function DelisleToKelvin($temperature)
  297. {
  298. return 373.15 - $temperature * 2 / 3;
  299. }
  300.  
  301. // Converts a given temperature from Delisle to Newton.
  302. function DelisleToNewton($temperature)
  303. {
  304. return 33 - $temperature * 11 / 50;
  305. }
  306.  
  307. // Converts a given temperature from Delisle to Rankine.
  308. function DelisleToRankine($temperature)
  309. {
  310. return 671.67 - $temperature * 6 / 5;
  311. }
  312.  
  313. // Converts a given temperature from Delisle to Reaumur.
  314. function DelisleToReaumur($temperature)
  315. {
  316. return 80 - $temperature * 8 / 15;
  317. }
  318.  
  319. // Converts a given temperature from Delisle to Romer.
  320. function DelisleToRomer($temperature)
  321. {
  322. return 60 - $temperature * 7 / 20;
  323. }
  324.  
  325. // Converts a given temperature from Fahrenheit to Celsius.
  326. function FahrenheitToCelsius($temperature)
  327. {
  328. return ($temperature - 32) * 5 / 9;
  329. }
  330.  
  331. // Converts a given temperature from Fahrenheit to Delisle.
  332. function FahrenheitToDelisle($temperature)
  333. {
  334. return (212 - $temperature) * 5 / 6;
  335. }
  336.  
  337. // Converts a given temperature from Fahrenheit to Kelvin.
  338. function FahrenheitToKelvin($temperature)
  339. {
  340. return ($temperature - 32) * 5 / 9 + 273.15;
  341. }
  342.  
  343. // Converts a given temperature from Fahrenheit to Newton.
  344. function FahrenheitToNewton($temperature)
  345. {
  346. return ($temperature - 32) * 11 / 60;
  347. }
  348.  
  349. // Converts a given temperature from Fahrenheit to Rankine.
  350. function FahrenheitToRankine($temperature)
  351. {
  352. return $temperature + 459.67;
  353. }
  354.  
  355. // Converts a given temperature from Fahrenheit to Reamur.
  356. function FahrenheitToReaumur($temperature)
  357. {
  358. return ($temperature - 32) * 4 / 9;
  359. }
  360.  
  361. // Converts a given temperature from Fahrenheit to Romer.
  362. function FahrenheitToRomer($temperature)
  363. {
  364. return ($temperature - 32) * 7 / 24 + 7.5;
  365. }
  366.  
  367. // Converts a given temperature from Kelvin to Celsius.
  368. function KelvinToCelsius($temperature)
  369. {
  370. return $temperature - 273.15;
  371. }
  372.  
  373. // Converts a given temperature from Kelvin to Delisle.
  374. function KelvinToDelisle($temperature)
  375. {
  376. return (373.15 - $temperature) * 3 / 2;
  377. }
  378.  
  379. // Converts a given temperature from Kelvin to Fahrenheit.
  380. function KelvinToFahrenheit($temperature)
  381. {
  382. return ($temperature - 273.15) * 9 / 5 + 32;
  383. }
  384.  
  385. // Converts a given temperature from Kelvin to Newton.
  386. function KelvinToNewton($temperature)
  387. {
  388. return ($temperature - 273.15) * 33 / 100;
  389. }
  390.  
  391. // Converts a given temperature from Kelvin to Rankine.
  392. function KelvinToRankine($temperature)
  393. {
  394. return $temperature * 1.8;
  395. }
  396.  
  397. // Converts a given temperature from Kelvin to Reaumur.
  398. function KelvinToReaumur($temperature)
  399. {
  400. return ($temperature - 273.15) * 4 / 5;
  401. }
  402.  
  403. // Converts a given temperature from Kelvin to Romer.
  404. function KelvinToRomer($temperature)
  405. {
  406. return ($temperature - 273.15) * 21 / 40 + 7.5;
  407. }
  408.  
  409. // Converts a given temperature from Newton to Celsius.
  410. function NewtonToCelsius($temperature)
  411. {
  412. return $temperature * 100 / 33;
  413. }
  414.  
  415. // Converts a given temperature from Newton to Delisle.
  416. function NewtonToDelisle($temperature)
  417. {
  418. return (33 - $temperature) * 50 / 11;
  419. }
  420.  
  421. // Converts a given temperature from Newton to Fahrenheit.
  422. function NewtonToFahrenheit($temperature)
  423. {
  424. return $temperature * 60 / 11 + 32;
  425. }
  426.  
  427. // Converts a given temperature from Newton to Kelvin.
  428. function NewtonToKelvin($temperature)
  429. {
  430. return $temperature * 100 / 33 + 273.15;
  431. }
  432.  
  433. // Converts a given temperature from Newton to Rankine.
  434. function NewtonToRankine($temperature)
  435. {
  436. return $temperature * 60 / 11 + 491.67;
  437. }
  438.  
  439. // Converts a given temperature from Newton to Reaumur.
  440. function NewtonToReaumur($temperature)
  441. {
  442. return $temperature * 80 / 33;
  443. }
  444.  
  445. // Converts a given temperature from Newton to Romer.
  446. function NewtonToRomer($temperature)
  447. {
  448. return $temperature * 35 / 22 + 7.5;
  449. }
  450.  
  451. // Converts a given temperature from Rankine to Celsius.
  452. function RankineToCelsius($temperature)
  453. {
  454. return ($temperature / 1.8) - 273.15;
  455. }
  456.  
  457. // Converts a given temperature from Rankine to Delisle.
  458. function RankineToDelisle($temperature)
  459. {
  460. return (671.67 - $temperature) * 5 / 6;
  461. }
  462.  
  463. // Converts a given temperature from Rankine to Fahrenheit.
  464. function RankineToFahrenheit($temperature)
  465. {
  466. return $temperature - 459.67;
  467. }
  468.  
  469. // Converts a given temperature from Rankine to Kelvin.
  470. function RankineToKelvin($temperature)
  471. {
  472. return $temperature / 1.8;
  473. }
  474.  
  475. // Converts a given temperature from Rankine to Newton.
  476. function RankineToNewton($temperature)
  477. {
  478. return ($temperature - 491.67) * 11 / 60;
  479. }
  480.  
  481. // Converts a given temperature from Rankine to Reaumur.
  482. function RankineToReaumur($temperature)
  483. {
  484. return ($temperature * 5 / 9 - 273.15) * 4 / 5;
  485. }
  486.  
  487. // Converts a given temperature from Rankine to Romer.
  488. function RankineToRomer($temperature)
  489. {
  490. return ($temperature - 491.67) * 7 / 24 + 7.5;
  491. }
  492.  
  493. // Converts a given temperature from Reaumur to Celsius.
  494. function ReaumurToCelsius($temperature)
  495. {
  496. return $temperature * 5 / 4;
  497. }
  498.  
  499. // Converts a given temperature from Reaumur to Delisle.
  500. function ReaumurToDelisle($temperature)
  501. {
  502. return (80 - $temperature) * 15 / 8;
  503. }
  504.  
  505. // Converts a given temperature from Reaumur to Fahrenheit.
  506. function ReaumurToFahrenheit($temperature)
  507. {
  508. return $temperature * 9 / 4 + 32;
  509. }
  510.  
  511. // Converts a given temperature from Reaumur to Kelvin.
  512. function ReaumurToKelvin($temperature)
  513. {
  514. return $temperature * 5 / 4 + 273.15;
  515. }
  516.  
  517. // Converts a given temperature from Reaumur to Newton.
  518. function ReaumurToNewton($temperature)
  519. {
  520. return $temperature * 33 / 80;
  521. }
  522.  
  523. // Converts a given temperature from Reaumur to Rankine.
  524. function ReaumurToRankine($temperature)
  525. {
  526. return $temperature * 9 / 4 + 491.67;
  527. }
  528.  
  529. // Converts a given temperature from Reaumur to Romer.
  530. function ReaumurToRomer($temperature)
  531. {
  532. return $temperature * 21 / 32 + 7.5;
  533. }
  534.  
  535. // Converts a given temperature from Romer to Celsius.
  536. function RomerToCelsius($temperature)
  537. {
  538. return ($temperature - 7.5) * 40 / 21;
  539. }
  540.  
  541. // Converts a given temperature from Romer to Delisle.
  542. function RomerToDelisle($temperature)
  543. {
  544. return (60 - $temperature) * 20 / 7;
  545. }
  546.  
  547. // Converts a given temperature from Romer to Fahrenheit.
  548. function RomerToFahrenheit($temperature)
  549. {
  550. return ($temperature - 7.5) * 24 / 7 + 32;
  551. }
  552.  
  553. // Converts a given temperature from Romer to Kelvin.
  554. function RomerToKelvin($temperature)
  555. {
  556. return ($temperature - 7.5) * 40 / 21 + 273.15;
  557. }
  558.  
  559. // Converts a given temperature from Romer to Newton.
  560. function RomerToNewton($temperature)
  561. {
  562. return ($temperature - 7.5) * 22 / 35;
  563. }
  564.  
  565. // Converts a given temperature from Romer to Rankine.
  566. function RomerToRankine($temperature)
  567. {
  568. return ($temperature - 7.5) * 24 / 7 + 491.67;
  569. }
  570.  
  571. // Converts a given temperature from Romer to Reaumur.
  572. function RomerToReaumur($temperature)
  573. {
  574. return ($temperature - 7.5) * 32 / 21;
  575. }
  576. ?>

Report this snippet  

You need to login to post a comment.