Page 1 of 1

Time question

Posted: Mon Jul 21, 2003 7:04 pm
by Testor
I was able to define:

$start_time = time();

On the first page to record time, then added this into a hidden field then on the next page I have:

$end_time = time();


I also have:

$duration = $start_time - $end_time;
$duration = date('m:s',$duration);

echoing $duration on the page doesn't give the desired result. Is there something else I should be doing to make the subtraction correct?

I know I should be using a javascript, but this should be simple enough to record the duration in an untimed quiz.

Thanks for your help.

...

Posted: Mon Jul 21, 2003 7:54 pm
by kettle_drum
Hmmm $start_time - $end_time will produce a negative number and will cause an error. This may be your error.

Also check to make sure that the time from the first page is begin passed properly - you may have global variables off and yet your not getting the variable from $_POST or $_GET.

Posted: Tue Jul 22, 2003 5:19 pm
by Testor
Thanks Kettle, but I managed to get it going. Here's what I did:

first.php:

<?php

$starttime=time();

?>
<form action="second.php" method="post">
<input type="hidden" name="starttime" value="<?php echo $starttime ?>">
<input type="submit" value"Submit">
</form>
---------------------------------------------
second.php:

<?php

$endtime=time();
$duration=($endtime - $starttime);
$h=($duration / 3600);
$i=($duration / 60 % 60);
$s=($duration % 60);
$duration = sprintf("%02d:%02d:%02d", $h, $i, $s);

echo "Duration time: $duration<p>\n";

?>
-------------------------------------------------
I'm sure someone with experience in PHP can re-write this in a more elegant and shorter format.