how to create a php stopwatch with start and finish button?

Asked

Viewed 2,872 times

0

preferably in php, but it can also be in javascript and please pass the values of time to a variable in php, because it will have an input to register the time taking to another page in php to register in the database.

thank you in advance.

  • 1

    I don’t know why they denied your question, but I already say that in PHP it is not possible to create a stopwatch (visually speaking), for this, the ideal is that it is in javascript. But to pass input to PHP, it requires a JSON request. But for what you want, in PHP, try reading about buffer or event_buffer_timeout_set

  • NOTE: I didn’t understand 100% what you want, I thought I was confused your question, but I put an answer below for an example of a chronometer with start/stop in javascript.

  • In PHP does not roll, only if you were to use ajax and keep pulling info all the time. The most recommended is jQuery or Javascript msm itself!

1 answer

2


In php I believe it is not what you want, since it runs on server.

I made a simple code in pure Javascript. Example working on Fiddle:

https://jsfiddle.net/dorathoto/5uswvep8/

But I don’t know if you want a timer or a countdown.

function formatatempo(segs) {
min = 0;
hr = 0;
/*
if hr < 10 then hr = "0"&hr
if min < 10 then min = "0"&min
if segs < 10 then segs = "0"&segs
*/
while(segs>=60) {
if (segs >=60) {
segs = segs-60;
min = min+1;
}
}

while(min>=60) {
if (min >=60) {
min = min-60;
hr = hr+1;
}
}

if (hr < 10) {hr = "0"+hr}
if (min < 10) {min = "0"+min}
if (segs < 10) {segs = "0"+segs}
fin = hr+":"+min+":"+segs
return fin;
}
var segundos = 0; //inicio do cronometro
function conta() {
segundos++;
document.getElementById("counter").innerHTML = formatatempo(segundos);
}

function inicia(){
interval = setInterval("conta();",1000);
}

function para(){
clearInterval(interval);
}

function zera(){
clearInterval(interval);
segundos = 0;
document.getElementById("counter").innerHTML = formatatempo(segundos);
}

<span id="counter">00:00:00</span><br>
<input type="button" value="Parar" onclick="para();"> <input type="button" value="Iniciar" onclick="inicia();"> <input type="button" value="Zerar" onclick="zera();">
  • I would implement to your code an ajax sending a signal to php saying when it started counting. So when the user clicked stop, he would send another ajax to php, and save the time as the subtraction of the two times, and compare it to the time that was sent by the form (accepting a difference of up to 2 seconds, taking into account the time of sending the request) . In this way it would avoid inconsistency or attempt to circumvent the system.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.