jacko_162 Posted September 23, 2014 Share Posted September 23, 2014 I have collected some data from .xml api's and I need to manipulate it in a way to display something specific. not my strongest point when it comes to maths & date data. so ultimately I need it to echo something like the following: "3 Days 4 Hours Remaining" or "14 Hours Remaining" (if below 24 hours!!!) I use the following to pull my variables and to test by echo results. <?php // Calculate total Fuel blocks $itemID = $record['itemID']; $result = mysql_query("SELECT * FROM `ecmt_poslistdetails` WHERE itemID = '$itemID' AND (typeID='4051' OR typeID='4247' OR typeID='4246' OR typeID='4312')"); $row = mysql_fetch_array($result); $fuelQty = $row[quantity]; echo $fuelQty; ?> <br /> <?php // Calculate total Fuel blocks used per hour $typeID = $record['typeID']; $result = mysql_query("SELECT * FROM `ecmt_posfuel` WHERE typeID = $typeID"); $row = mysql_fetch_array($result); $fuelUse = $row['fuel']; echo $fuelUse; ?> this gives me: $fuelQty & $fuelUse so for instance if fuel quantity is: 18480 and $fuelUse is: 40 the fuel used is 40x per hour and time remaining would be 462x Hours remaining but I want it to display: 19 Days 6 Hours Remaining how can I achieve this format? Link to comment https://forums.phpfreaks.com/topic/291233-calculating-time-left-using-2x-int-values/ Share on other sites More sharing options...
Barand Posted September 23, 2014 Share Posted September 23, 2014 let the dateTime and dateInterval classes do the work for you $fuel = 18480; $fuelUse = 40; $hrs = $fuel/$fuelUse; $dt1 = new DateTime("+$hrs hours"); $dif = $dt1->diff(new DateTime()); // $dif is a dateInterval object echo $dif->d . ' days '; echo $dif->h . ' hours '; echo $dif->m . ' minutes'; Link to comment https://forums.phpfreaks.com/topic/291233-calculating-time-left-using-2x-int-values/#findComment-1491872 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.