Jump to content

calculating time left using 2x INT values


jacko_162

Recommended Posts

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?

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';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.