How to show the seconds running in the date function

Asked

Viewed 33 times

0

Good morning guys, how do I show the seconds running in the function below? can be some function in jQuery to animate php.

date_default_timezone_set('America/sao_paulo');
//CRIA UMA VARIAVEL E ARMAZENA A HORA ATUAL DO FUSO-HORÀRIO DEFINIDO (BRASÍLIA)
$Asuncion = date('H:i:s', time()); 

1 answer

2

You can do via jquery by picking up the client’s time.

<!DOCTYPE html>
<html>
<body>

    <p>A script on this page starts this clock:</p>
    <p id="demo"></p>

    <button onclick="myStopFunction()">Stop time</button>

    <script>
    var myVar = setInterval(myTimer, 1000);

    function myTimer() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        document.getElementById("demo").innerHTML = t;
    }

    function myStopFunction() {
        clearInterval(myVar);
    }
</script>

</body>
</html>

Reference: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_setinterval_clearinterval

Or use setInterval to call a function that performs the ajax request to pick up the server time in PHP.

I hope I’ve helped.

  • Thanks Roberto for the contribution but in this case the system takes the time of the server or the client? and how do I change the time zone? example: take NY time?

  • In this case, it’s customer time. To catch the time from another place I believe I would have to return getTime from the current time and make a sum or subtraction depending on which location you need. Usually use momentjs to work with date

Browser other questions tagged

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