This is a bit late, I know, as I only just now came to this forum on a random chance.
My suggestion is to do what Gmail does. Every one minute, save the current time. Then you can tell when the user left the website within 1 min. accuracy. If you need a greater or less accuracy, then just adjust the time interval.
2 good sources for "AJAX" are.
in quick Pseudo code it would look like thing like this.
$Timestamp = $_REQUEST['timestamp'];
$query = "INSERT INTO MyTable Timestamp = timestamp";
Every x seconds
Sorry, I don't have time to execute the pseudo code for the "AJAX" part, but you can easily check out the examples in the above two links to understand that.