I think your title is a little misleading although your post is tagged correctly. None of this (unless you use AJAX) will be in PHP. Your best be would probably to use timeouts with keyup or keydown events -- restart the timeout timer every time the event is fired. If the time runs out, you stop the global timer that keeps track of everything. I'm not sure I fully understand
Someone else also suggested a time out limit but what happens if it times out and the
person returns to finish their typing.
Isn't that the idea? They would take some sort of break during which the timer would pause and then resume when they return? If that is the case, it will be very difficult to determine what a "break" is versus what a "finished" is. In that case you would need to define some constant timeout after which it is assumed they are done. Otherwise, you will probably need some sort of "I'm done" button or event that either is clicked or fired based on some user action.
Edit in response to your comment: Basically, you have a couple of options. You will need at least one AJAX call if you want to store the time data on the server. This will fire when the "Done" button gets clicked. You will need JS handlers to handle the keydown/keyup events. These can either trigger AJAX calls which in turn start/stop timers on the server written in PHP. Alternatively you could have these start/stop timers on the client implemented in JS. Basically, the way I see it, (this is pesudocode, not actual JS)
function keyDownHandler{
resumeGlobalTimer();
restartTimeOutTimer();
}
function timeout(){
pauseGlobalTimer();
}
function resumeGlobalTimer(){
if (globalTimerIsRunning)
//global timer wasn't paused, do nothing
else{
someTimer.resume();
globalTimerIsRunning = true;
}
}
So basically, when a key is pressed, you start a timer that waits to see if another key is pressed within the time limit and it also calls a function that resumes the global timer. If a key is pressed within the time limit, that timeout timer is reset to the starting value and starts ticking down again. If it times out, it assumes the person walked away and pauses the global timer. The resume function checks to make sure the timer isn't already running (ie, not paused) and then attempts to resume it.
At the end of everything, an AJAX call will upload the final global value to PHP.
Hope this helps!