Jump to content

calculations with timestamp


i8grand

Recommended Posts

its not working just doing it like that :S

 

		$dbQuery 	= 	"SELECT startTime, endTime FROM puzzlestatus WHERE puzzleID=$puzzle";
	$result 	= 	mysql_query($dbQuery,$db);
	$dbRow 		= 	mysql_fetch_array($result);	

		$start 		= 	$dbRow[0];
		$end 		= 	$dbRow[1];

	echo "Start: $start"."<br>";
	echo "End: $end"."<br>";

	$totalTime = ($end-$start);

	echo "Total: $totalTime"."<br>";

 

$start and $end both come echo what the correct timestamps but $totalTime just echoes 0.

 

Am i doing something silly, can anyone help?

 

Dylan.

A valid question. The word 'timestamp' without any more context is often assumed to mean Unix timestamp - a number of secoonds since 1970-01-01 GMT. If it's MySQL's TIMESTAMP, it get's returned as Y-M-D H:M:S format, which obviously cannot be treated arithmetically.

 

You can do:

$totalTime = strtotime($endTime) - strtotime($startTime);

to get number of seconds, then extract seconds/minutes/hours/days

 

$totalTime = strtotime($endTime) - strtotime($startTime); //let's say it's 123456
$days = floor($totalTime / (24 * 3600)); // there are 3600 seconds in an hour
$totalTime = $totalTime %  (24 * 3600);
$hours = floor($totalTime / 3600);
$totalTime = $totalTime %  (3600);
$minutes = floor($totalTime / 60);
$seconds = $totalTime %  60;

$time = "$days : $hours : $minutes : $seconds"; //'1 : 10 : 17 : 36 '

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.