Jump to content

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 '

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.