Antes de ejecutar la prueba, debe calcular la hora exacta de finalización de la prueba en el lado del servidor y guardarla en la sesión:
<?php
if (empty($_SESSION['test_will_end_by'])) {
$_SESSION['test_will_end_by'] = time() + $test_duration_in_seconds;
}
?>
Luego, si lo proporciona a su secuencia de comandos del lado del cliente desde HTML:
Time left:
<span class="timer" data-end="<?php
echo date(DateTime::RFC1123, $_SESSION['test_will_end_by']);
?>"></span>
Agregue este código en su controlador jQuery DOM-ready para iniciar todos los temporizadores en una página:
$('.timer').each(function() {
var target = new Date($(this).data('end')), update, $this = $(this);
(update = function () {
var now = new Date();
$this.text((new Date(target - now)).toUTCString().split(' ')[4]);
if (Math.floor((target - now)/1000) == 0) return; // timer stops
setTimeout(update, 1000);
})();
});