SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot Mahz's Avatar
    Join Date
    Mar 2006
    0 Post(s)
    0 Thread(s)

    Keeping a displayed timer updated realtime

    I'm sort of making a game from the ground up to learn my way around ajax and its interaction with php and myself. basically, I want to have a master timer on the server (independent of server time) and have certain things happen for the user depending on what time it is. I want to display the master time on a page and have it update realtime by the second but I can not figure out how to do this.

    To start off with something basic, how would I go about keeping a realtime updating timer that's being run on the server to display on a page and update per second? What's the best way of keeping a sort of master counter stored on a server? I'm using PHP and MYSQL.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Birtley, UK
    0 Post(s)
    0 Thread(s)
    paste this into a php file and upload it to your server.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    <html xmlns="">
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                var time;
                function setup() {
                    time = new Date(<?php echo time(); ?> * 1000);
                    setTimeout('updateTime()', 1000);
                function updateTime() {
                    setTimeout('updateTime()', 1000);
                    document.getElementById('test').value = time;
                window.onload = setup;
            <input type="text" id="test" style="width: 300px;" />
    When the page is loaded the server time is writen into a javascript date, and that is then updated every second.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts