Log in

View Full Version : How to count time from when user load a page/form until they submit it?



k12onos
03-18-2012, 04:05 AM
Hi :)

I'm trying to create a quiz using php & html forms, I am planning to count the time used by the user to complete the form, and then posting it in the database and producing a highscore list sorted by the actual quiz score and completion time.

The problem is, I don't know how to count the time from when user loads the page of the quiz and submit the form..

Could anyone give me some pointers?

Thanks in advance, I would very much appreciate your help :)

keyboard
03-18-2012, 05:47 AM
Php can only interact with the webpage before it loads so it would be hard to make a timer that runs off php. Using javascript, this would be simple enough.
Google javascript countdown.

james438
03-18-2012, 06:50 AM
I think you are misunderstanding his question keyboard1333.

Just create a database with a unique value for the user like a cookie and then store the cookie in the database under the field cookie and next to it store the login datetime. When the quiz is completed the user will click the submit button. At this point the datetime value is stored in the database and the difference between the two database values is retrieved and displayed. If you want you could then store the time to complete the quiz in the database as well in case the person wants to try again and beat their current score.

keyboard
03-18-2012, 08:03 PM
Hi :)
The problem is, I don't know how to count the time from when user loads the page of the quiz and submit the form..


I assumed he meant a visible countdown on the page so the user can see how long the're taking. Wouldn't that be more user friendly as it would make them try harder to beat their score? And you can still calculate the overall time.

james438
03-18-2012, 08:14 PM
It is possible that is what he meant, but he mentioned php, form, and database as well as displaying the results in the form of a list. This led me to think he was not talking about a javascript timer. Either method should work.

The javascript method may be more user friendly. I personally prefer it without, but that's just me.

keyboard
03-18-2012, 08:17 PM
Hey k12onos, if you want us to post some actual code, to help get you started, could you please define whether you want a live counter (javascript) an overall time it took (php) or both.

k12onos
03-20-2012, 12:19 PM
Hi, thank you for you replies, I'm sorry for the late response.

Actually I have done some research but unfortunately I couldn't find the javascript countdown you mentioned.

But, I managed to find something with PHP that serves the basic function that I am searching.

I use this in my starting page


<?php $timestart=time(); ?>

<input type="hidden" name="timestart" value="<?echo $timestart; ?>">



And this in my results page


$timestart = $_POST['timestart'];

$timeend=time();

$used_time=$timeend-$timestart;
$used_time=date('s',$used_time);


This is functional, but does not take account of the loading time of the page (since my quiz uses quite lots of images, loading time may vary due to user's internet speed), and thus, it puts those with slower internet at a disadvantage.



Actually, I am more interested in the Live Counter you are talking about. Indeed it will make it more interesting :)

Is this strictly only achievable through Javascript? If possible can you give me more pointers for me to make this live counter? or should I create a new thread at Javascript forum to prevent this going too much out of topic?

Anyway, thanks a lot for the responses keyboard1333 and james438 :)