Time question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Testor
Forum Newbie
Posts: 12
Joined: Tue Jul 16, 2002 11:26 am

Time question

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

...

Post 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.
Testor
Forum Newbie
Posts: 12
Joined: Tue Jul 16, 2002 11:26 am

Post 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.
Post Reply