Posted By

Scooter on 05/09/09


Tagged


Versions (?)

Fuel consumption


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com/2009/05/fuel-consumption.html

  1. <?php
  2. /*
  3.   Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
  4.  
  5.   This work is licensed under the Creative Commons Attribution License. To view
  6.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  7.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  8.   94305, USA.
  9.   */
  10.  
  11. /*
  12.   NOTE: The "magic numbers" in these functions are from the 2007 Fuel Consumption Guide
  13.   published by Natural Resources Canada and use a conversion of 4.546 L = 1 imperial gallon.
  14.   U.S. fuel economy ratings use U.S. gallons which are 20% smaller than imperial gallons.
  15.   */
  16.  
  17. // Calculate fuel consumption rating in litres per 100 kilometres
  18. function lph($kilometres, $litres)
  19. {
  20. return ($litres * 100) / $kilometres;
  21. }
  22.  
  23. // Calculate fuel consumption rating in miles per gallon.
  24. function mpg($miles, $gallons)
  25. {
  26. return $miles / $gallons;
  27. }
  28.  
  29. // Convert miles per (imperial) gallon to litres per 100 kilometres.
  30. function lph2mpg($mpg)
  31. {
  32. return 282.48 / $mpg;
  33. }
  34.  
  35. // Convert litres per 100 kilometres to miles per (imperial) gallon.
  36. function mpg2lph($lph)
  37. {
  38. return 282.48 / $lph;
  39. }
  40.  
  41. // Calculate fuel consumption in litres.
  42. /*
  43.   distanceTravelled = the distance travelled in kilometres; e.g. 20000
  44.   percentCity = the percentage of the distance travelled occuring in the city; e.g. 0.55
  45.   percentHighway = the percentage of the distance travelled occuring on the highway; e.g. 0.45
  46.   ratingCity = city fuel consumption rating in litres per 100 kilometres; e.g. 7.9
  47.   ratingHighway = highway fuel consumption rating in litres per 100 kilometres; e.g. 5.9
  48.   */
  49. function fuelConsumption($distanceTravelled, $percentCity, $percentHighway, $ratingCity, $ratingHighway)
  50. {
  51. $cityConsumption = ($distanceTravelled * $percentCity * $ratingCity) / 100;
  52. $highwayConsumption = ($distanceTravelled * $percentHighway * $ratingHighway) / 100;
  53. $fuelConsumption = $cityConsumption + $highwayConsumption;
  54. }
  55.  
  56. // Calculate carbon dioxide emissions in kilograms.
  57. function CO2emissions($fuelConsumption, $fuelType)
  58. {
  59. return $fuelConsumption * (($fuelType == 'diesel') ? 2.7 : 2.4);
  60. }
  61. ?>

Report this snippet  

You need to login to post a comment.