snip77 snip77 - 2 months ago 17
HTML Question

Update time in php

I have a div taq in my site and I put time() in it with Php
Now I want to update label each 1 second
But there is no timer in Php.
How can I update it?
I want to update 1 second 1second without refresh.

Answer

I'm having a pretty hard time understanding what you are looking for, however, I read what you asked and I gathered that you were looking to get a timer.

PHP wouldn't be able to provide such a function as it's server sided and doesn't touch the user/client's browser.

You should be able to use a timer in JavaScript however.

<label id="minutes">00</label>:<label id="seconds">00</label>
    <script type="text/javascript">
        var minutesLabel = document.getElementById("minutes");
        var secondsLabel = document.getElementById("seconds");
        var totalSeconds = 0;
        setInterval(setTime, 1000);

        function setTime()
        {
            ++totalSeconds;
            secondsLabel.innerHTML = pad(totalSeconds%60);
            minutesLabel.innerHTML = pad(parseInt(totalSeconds/60));
        }

        function pad(val)
        {
            var valString = val + "";
            if(valString.length < 2)
            {
                return "0" + valString;
            }
            else
            {
                return valString;
            }
        }
    </script>

Comments