-1

I have a page that shows the status of a TeamSpeak server on which I user ServerQuery to get the server's uptime using $uptime. This displays the server uptime as a total in seconds and I convert it to days, hours, minutes and seconds using

function secondsToTime($seconds) {
    $dtF = new \DateTime('@0');
    $dtT = new \DateTime("@$seconds");
    return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');

I output the uptime in the above format using secondsToTime($uptime); but I would like for the output to count up from the original output once the page is loaded instead of having to refresh the page to get the new uptime.

Thanks in advance.

JakeGriffin
  • 223
  • 1
  • 6
  • 17
  • Use `new DateTime()` instead of `new DateTime('@0')`. – ForceMagic Jul 17 '16 at 18:04
  • @ForceMagic I was quite vague in my question, I've updated it to make more sense. The code posted works fine and isn't the issue. I would like to count up from the time output from the code above. – JakeGriffin Jul 17 '16 at 18:08
  • You should do that client-wise. If you're trying to do that sever-side, you're wasting your time. JavaScript might be a solution to this, let me post an example... – ForceMagic Jul 17 '16 at 18:10
  • If you want the time to be constantly updating without page reload, the you would need JavaScript. If you wanted to get the time from PHP then some Ajax could be implemented. – Matt Inamdar Jul 17 '16 at 18:10
  • I just assumed it could be done server side to ensure the same time for every client - if you could post a JavaScript example that would be great! – JakeGriffin Jul 17 '16 at 18:15
  • A very simple google search would give you literally thousands of tutorials – rjdown Jul 17 '16 at 18:17
  • Possible duplicate of [Javascript and PHP countdown Timer that displays the same for everyone](http://stackoverflow.com/questions/12486885/javascript-and-php-countdown-timer-that-displays-the-same-for-everyone) – Jocelyn Jul 17 '16 at 18:31

1 Answers1

1

You should use JavaScript to make a simple timer.

var timeelm, time, days, hours, minutes, seconds;
timeelm = document.getElementById("time");
time = timeelm.innerHTML;
days = parseInt(time.split(" ")[0]);
hours = parseInt(time.split(" ")[2]);
minutes = parseInt(time.split(" ")[4]);
seconds = parseInt(time.split(" ")[7]);
timerGo();

function timerGo() {
    seconds++;
    if (seconds == 60) {
            minutes++;
        seconds = 0;
    }
    if (minutes == 60) {
            hours++;
        minutes = 0;
    }
    if (hours == 24) {
            days++;
        hours = 0;
    }
    timeelm.innerHTML = days+" days, "+hours+" hours, "+minutes+" minutes and "+seconds+" seconds";
    setTimeout(timerGo, 1000);
}

Note: Make sure that your webpage has an element assigned an ID of time.

Check out this fiddle.

ForceMagic
  • 516
  • 8
  • 19
  • Unfortunately, this is at the mercy of the page having focus, the PC not lagging etc. (timeout is notoriously inaccurate). Better to recalculate from the original time, or the countdown will quickly become wrong. – rjdown Jul 17 '16 at 18:38
  • But if a user visits a page, he/she could always refresh the page to get the millisecond exact calculations. And because we're using JavaScript, there is no way that a server/ajax is processing this information. – ForceMagic Jul 17 '16 at 18:43
  • There's not much point having a countdown if they have to refresh to see what the countdown is (because it WILL quickly become inaccurate). You have the original time, you can easily get the current time. Just calculate the difference, all in JS. As I said above there are thousands of tutorials. – rjdown Jul 17 '16 at 18:47
  • Oh my gosh, why didn't I think of that?! But never mind, the thing is, a quick google search and he has like billions of results :P – ForceMagic Jul 17 '16 at 18:48