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.
Time question
Moderator: General Moderators
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
...
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.
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.
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.
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.