SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using JavaScript to display date and time

    I posted this on the PHP forum and was advised to seek a JavaScript solution.

    * * * * * * * * * *

    If you look in the top right corner of my page at http://www.politix.us/world/eur/afg/index.php you'll see a PHP function that displays the date and time. This is the code behind it:

    PHP Code:
    <div id="pagedate"> (<em><?php echo date("l"); ?></em>) <?php echo date("F d, Y"); ?> <span id="pagetime"><?php echo date("h:i:s A"); ?></span></div>
    I have two questions:

    1. How can I make it dynamic, so that the minutes and seconds change even if a page isn't refreshed?

    2. How can I make it display the VIEWER'S local time? It shows midnight when it's 9 p.m. my time, or something like that.

    Also, would displaying seconds eat up a lot of bandwidth? If so, I might limit it to minutes.

    Thanks.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi geosite, this is a quick example but should give you some ideas:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html>
    <head>
    <script type='text/javascript'>
    window.onload = function()
    {
      if (document.getElementById) {
        rtClock();
      }
    }
    var dayOfWeek = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    
    function rtClock()
    {
      var m = 'AM', now = new Date();
      setTimeout('rtClock()', 1000);
      var ele = document.getElementById('clock');
      var hours = now.getHours();
      if (hours == 0) hours = 12;
      else if (hours == 12) m = 'PM';
      else if (hours > 12) {
        hours -= 12;
        m = 'PM';
      }
      ele.innerHTML = dayOfWeek[now.getDay()] + ', ' +
                      hours + ':' +
                      now.getMinutes() + ':' +
                      now.getSeconds() + ' ' + m;
    }
    </script>
    </head>
    <body>
    
    
    <div id='clock'></div>
    
    </body>
    </html>

  3. #3
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also look for something in here...
    http://www.hotscripts.com/JavaScript...ime/index.html

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tips. I may use PHP to display the date, since it's so much simpler, then use JavaScript to make a dynamic time display.


Bookmarks

Posting Permissions

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