[Solved] Timer recorded in php


You could use setInterval to increment some number every second, starting the interval when the input gets focus, and clearing the interval when it loses focus.

    var timeSpent = 0;
    var interval;
    document.getElementById('password2').addEventListener('focus', function() {
        interval = setInterval(function() { 
            timeSpent++;
            document.getElementById('timeSpent').value = timeSpent;
        }, 1000);
    });
    document.getElementById('password2').addEventListener('input', function(event) {
        if (interval && event.target.value === document.getElementById('password').value) {
            clearInterval(interval);
        }
    });
    document.getElementById('password2').addEventListener('blur', function() {
        if (interval) {
            clearInterval(interval);
        }
    });
Password: <input id="password" type="password"/><br/>
Reenter Password: <input id="password2" type="password"/><br/>
Time Spend: <input id="timeSpent" type="number"/><br/>

Then in PHP you will have something like:

$link = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);

$statement = $link->prepare("INSERT INTO users(name, password_hash, time_spent)
VALUES(:name, :hash, :time)");

$statement->execute(array(
    "name" => $_POST['username'],
    "hash" => password_hash($_POST['password'], PASSWORD_DEFAULT),
    "time" => $_POST['timeSpent']
));

1

solved Timer recorded in php