Estimate time spent on each question in php

I'm working on improving the quiz  module ,for example student attempt each question and
each time the button "submit" is clicked it save  id ,answer, qid, in the table called  "child _attempt table"
I would like to implement  time  has to  spent answering each question . how to track start time and end time for each question using php


[b]Parent_attempt table[/b]

id,	eid,	studentd_id,	attempted_number,	start_date,	end_date,	   is_submitted,	score	

[b]child _attempt table[/b]

id,	qid,	attempted_id,	  answer	

Open in new window

prashanth agsoftware engineer Asked:
Who is Participating?
 
Ray PaseurCommented:
Here's an example.  You can try it on my server here, or install it and run it on your server.
http://iconoun.com/demo/temp_prasan.php

<?php // demo/temp_prasan.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28645022.html
 */
error_reporting(E_ALL);

session_start();

if (!empty($_POST))
{
    $lapse = time() - $_SESSION['alpha'];
    echo PHP_EOL . "IT TOOK $lapse SECONDS TO SUBMIT THE FORM";
}

$_SESSION['alpha'] = time();
$form = <<<EOD
<form method="post">
Click this button to submit the form: <input name="x" type="submit" />
</form>
EOD;

echo $form;

Open in new window

0
 
kyodaiCommented:
If you use a database you could store the current time when the page is loaded and then store a second value to save the time some button is clicked (The answering button or so?) then you can calculate with the results...


Just use "DATE" to get the current timestamp.

date("Y-m-d H:i:s");
0
 
prashanth agsoftware engineer Author Commented:
I tried this logic but it shows zero difference

$time = time();
$_SESSION['time_started'] = $time;

$endtime = $_SESSION['time_started'];

$starttime = time();

$difference = $starttime - $endtime;
echo $difference;

Open in new window

0
 
Chris StanyonCommented:
That logic won't show any difference - the $time and $starttime are set at the same time, in the same script !!

You need to set the starttime when you load the question page:

<?php
session_start();
$_SESSION['starttime'] = time();
?>

Open in new window


Then when you submit the question, presumably by submitting it to another php script you can calculate the difference by subtracting the starttime from the endtime. Don't forget to reset the starttime

<?php
session_start();
$timespent = time() - $_SESSION['starttime'];
unset($_SESSION['starttime']);
echo $timespent;
?>

Open in new window


You might also want to add in some error checking to make sure that the session variables are set
0
 
prashanth agsoftware engineer Author Commented:
Dear Ray Paseur,

excellent code ,thanks for sharing
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.