I'm trying to make a "game" of sorts on my local browser. The idea is to present a number or word to the player, and then the player will enter the associated word (if what is presented is a number) or number (if what is presented is a word). I've done that easily enough, and I'm using a database to access these words.
Now what I'd like to do is measure the time the player takes to enter the answer for each question (not for each game -- I could just sum all the question times to get that), then insert the values into the database.
I've got a script running, but the measured time is showing me a negative value. Here's the script (explanation at the bottom)
<?php
try{
$handler = new PDO('mysql:host=127.0.0.1;dbname=databasename', 'username', 'password');
$form_uuid = uniqid();
$_SESSION['Qtime'][$form_uuid] = round(microtime(true) * 1000);
} catch(PDOException $e) {
die($e->getMessage());
}
##########################################################
##################### GAME MODULE ########################
#error_reporting(0);
$id = mt_rand(0,99);
$rev = mt_rand(0,5); //reverse, so 0 asks for id and 1 asks for peg
$random = $handler->query('SELECT * FROM major WHERE id='.$id);
if($rev < 3){
$question = $random->fetch(PDO::FETCH_ASSOC)['peg'];
} else {
$question = $random->fetch(PDO::FETCH_ASSOC)['id'];
}
#echo '<pre>', print_r($handler->query('SELECT * FROM major WHERE id='.$id)->fetch(PDO::FETCH_ASSOC)), '</pre>';
echo '<pre>', print_r($_POST), '</pre>';
#echo $rev;
echo '<h1>';
if(!empty($_POST)){
echo 'Next: ';
}
echo $question, '</h1>';
$P_rev = $_POST['rev'];
if($P_rev < 3){
$sql = "SELECT * FROM major WHERE id='".$_POST['guess']."' AND peg='".$_POST['Q']."'";
} else {
$sql = "SELECT * FROM major WHERE id='".$_POST['Q']."' AND peg='".$_POST['guess']."'";
}
#echo $sql;
$check = $handler->query($sql)->fetch();
if($check){
echo '<h2>Correct!</h2>';
} else {
if(!empty($_POST)){
echo '<h2>Wrong</h2>';
}
}
?>
<form action="flashcards.php" method="post">
<input type="text" name="guess" id="guess" /><br/>
<input type="submit" value="Go" id="submit" />
<input type="hidden" name="Q" value="<?php echo $question; ?>" />
<input type="hidden" name="rev" value="<?php echo $rev; ?>" />
<input type="hidden" name="form_id" value="<?php print $form_uuid; ?>">
<input type="hidden" name="end_time" value="<?php echo round(microtime(true) * 1000) ?>">
</form>
<?php
#echo '<pre>', print_r($_POST), '</pre>';
echo '<pre>', print_r($_SESSION), '</pre>';
#echo $form_uuid, '<br/>';
$form_uuid = $_POST['form_id'];
#echo $form_uuid;
#echo '<pre>', print_r($_SESSION), '</pre>';
$key = key($_SESSION['Qtime']);
$start_time = $_SESSION['Qtime'][$key];
// Do other form processing here..
$end_time = $_POST['end_time'];
echo $end_time - $start_time;
?>
I've got a lot of echos in there for debugging purposes. Anyway:
What I did was get the session time using
$form_uuid = uniqid(); $_SESSION['Qtime'][$form_uuid] = round(microtime(true) * 1000);
The unique id allows me to differentiate between this question and the previous question. So then, this should give me the time at the beginning of the page refresh.
So far so good. But the issue comes when I try to record the end time. I record it by putting this in my form:
<input type="hidden" name="end_time" value="<?php echo round(microtime(true) * 1000) ?>">
My impression is that this is supposed to give me the time when the form is submitted, right? Which means it measures the page submit time. But when I get the difference:
$key = key($_SESSION['Qtime']);
$start_time = $_SESSION['Qtime'][$key];
// Do other form processing here..
$end_time = $_POST['end_time'];
echo $end_time - $start_time;
The echo is negative.
I have two ideas why, both of which seem to be wrong:
Maybe I'm subtracting the start time of this question with the end time of the previous question. It makes sense because I'm storing the page load time of the current question in the session, and then subtracting it from the submit time in the form.
In that case, this must be page load time I'm getting. Page load time of the current question minus page submit time of the previous question -- only the page refresh occurs in that interval.
But I know I'm wrong because when I hit the submit button, the magnitude of the answer is dependent on how long I take to click submit. So if I take my time, the answer can go in the negative thousands, whereas if I click as quickly as possible, it just stays in the negative one hundreds to two hundreds.
It can't be page refresh time.
So idea 2 is that this may be the actual value I want, which is page submit minus page load. But then, why is it negative? The negative sign seems to tell me I'm getting something in reverse... but I have no ideas.
What's happening in this script?