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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 StanyonWebDevCommented:
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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prashanth agsoftware engineer Author Commented:
Dear Ray Paseur,

excellent code ,thanks for sharing
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.