Solved

Server DateTime dispaly PHP

Posted on 2006-06-16
4
309 Views
Last Modified: 2008-01-09
Hai experts,

  I want example script to display datetime in webpage without refreshing tha page, the server time will dispalyed and each second it will changed it , i need this in PHP
Example URL
 http://ntp.greenwichmeantime.com/time/scripts/clock-6-1/clock.php?tzone_choice=main_large

Thanks
Bala
0
Comment
Question by:gstalin
  • 2
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16921491
Hi,

I think you should be looking for a javascript solution. Using every second reload with PHP is pushing unnecessairy traffic on your server. A sample javascript is this:

/*
 * Use a javacript based clock in your webpages.
 * The clock supports date formatting as known
 * from the php date() function.
 *
 * One clock instance is used to update several listeners
 *
 * <sample>
 *   <div id="myClock">Loading clock..</div>  
 *   <script type="text/javascript">
 *     // load the Clock
 *     c = new Clock();
 *     // attach a listener which loads and updates the time
 *     // in a HTML element with id="myClock", using the
 *     // dd-mm-yyyy hh:mm format.
 *     c.addListener(new ClockListener("myClock", "d-m-Y H:i"));
 *   </script>
 * </sample>
 */
 
  var mainClock = false;

  function Clock()
  {
    if(mainClock) return mainClock;
    mainClock = this;
    this.listeners = new Array();
   
    this.minInterval = 1000 * 60; //every minute;
    this.interval = false;
    this.lmonths  = ["", "January","February","March","April","May","June","Juli","August","September","October","November","December"];
    this.smonths  = ["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
    this.Svals    = ["st", "nd", "rd", "th"];
    this.addListener = function(l)
    {
      if(l.format.match(/s/) && this.minInterval > 1000)
      {
        this.minInterval = 1000;
        this.doInterval();
      } else if(!this.interval)
      {
        this.doInterval();
      }
      this.listeners[this.listeners.length] = l;
      this.update(l);
    }
   
    this.update = function(l)
    {
      var d  = new Date();
      this.lcJ = d.getDate();
      this.lcD = this.lcJ < 10 ? "0" + this.lcJ : this.lcJ;
      this.lcW = d.getDay();
      this.lcN = d.getMonth()+1;
      this.lcM = this.lcN < 10 ? "0" + this.lcN : this.lcN;
      this.ucY = d.getFullYear();
      this.lcY = d.getYear();
      this.lcG = ((d.getHours() - 1) % 12) + 1;
      this.ucG = d.getHours();
      this.lcH = this.lcG < 10 ? "0" + this.lcG : this.lcG;
      this.ucH = this.ucG < 10 ? "0" + this.ucG : this.ucG;
      this.lcI = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
      this.lcS = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
      this.lcA = d.getHours() < 12 ? 'am' : 'pm';
      this.ucA = d.getHours() < 12 ? 'AM' : 'PM';
      this.ucF = this.lmonths[this.lcN];
      this.ucM = this.smonths[this.lcN];
      this.ucS = this.Svals[this.lcJ] ? this.Svals[this.lcJ] : 'th';
     
      if(l) {     //Update single Listener
        l.update(this);
      } else {    //Update all Listeners
        for(i = 0; i < this.listeners.length; i++)
          this.listeners[i].update(this);
      }
    }
    this.doInterval = function()
    {
      clearInterval(this.interval)
      this.interval = setInterval('_clockInterval()', this.minInterval);
    }
  }
 
  function _clockInterval()
  {
    if(mainClock)
      if(mainClock.update)
        mainClock.update();
  }
 
  function ClockListener(elemId, phpDateFormat)
  {
    this.elemId = elemId;
    this.format = phpDateFormat;
    this.update = function(clk)
    {
      if(document.getElementById)
        if(document.getElementById(this.elemId))
        {
          str = this.format;
          if(this.format.indexOf('h') > -1) str = str.replace('h', clk.lcH);
          if(this.format.indexOf('j') > -1) str = str.replace('j', clk.lcJ)
          if(this.format.indexOf('d') > -1) str = str.replace('d', clk.lcD)
          if(this.format.indexOf('w') > -1) str = str.replace('w', clk.lcW)
          if(this.format.indexOf('n') > -1) str = str.replace('n', clk.lcN)
          if(this.format.indexOf('m') > -1) str = str.replace('m', clk.lcM)
          if(this.format.indexOf('Y') > -1) str = str.replace('Y', clk.ucY)
          if(this.format.indexOf('y') > -1) str = str.replace('y', clk.lcY)
          if(this.format.indexOf('g') > -1) str = str.replace('g', clk.lcG)
          if(this.format.indexOf('G') > -1) str = str.replace('G', clk.ucG)
          if(this.format.indexOf('H') > -1) str = str.replace('H', clk.ucH)
          if(this.format.indexOf('i') > -1) str = str.replace('i', clk.lcI)
          if(this.format.indexOf('s') > -1) str = str.replace('s', clk.lcS)
          if(this.format.indexOf('S') > -1) str = str.replace('S', clk.ucS)
          if(this.format.indexOf('a') > -1) str = str.replace('a', clk.lcA)
          if(this.format.indexOf('A') > -1) str = str.replace('A', clk.ucA)
          if(this.format.indexOf('M') > -1) str = str.replace('M', clk.ucM)
          if(this.format.indexOf('F') > -1) str = str.replace('F', clk.ucF)
          document.getElementById(this.elemId).innerHTML = str;
        }
    }
  }

I hope this helps,

-r-
0
 

Author Comment

by:gstalin
ID: 16925499
Hai Roonaan ,
This javascript code ok,but how to i pass my PHP date to this code can you give how to integerate or pass parameter ,calling funtion for this code with HTML emements, and how to add listener,

Thanks
Bala
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
ID: 16925632
Hi,

I adjusted the code for you. You can change the loadClocks() function to set the clock to a specific time. I set it to 12/12/2006, but for exact servertime you can use:
c.setTimeStamp(<?php echo mktime();?>);

[ServerClock.php]
/*
 * Use a javacript based clock in your webpages.
 * The clock supports date formatting as known
 * from the php date() function.
 *
 * This clock can be set to a specific time from serverside
 *
 * You can move most of the Clock code in an external JS
 * file.
 */
 
<html>
  <head>
  <script type="text/javascript">
  var mainClock = false;

  function Clock()
  {
    if(mainClock) return mainClock;
    mainClock = this;
    this.listeners = new Array();
    this.minInterval = 1000 * 60; //every minute;
    this.interval = false;
    this.lmonths  = ["", "January","February","March","April","May","June","Juli","August","September","October","November","December"];
    this.smonths  = ["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
    this.Svals    = ["st", "nd", "rd", "th"];
    this.diff     = 0;
    this.addListener = function(l)
    {
      if(l.format.match(/s/) && this.minInterval > 1000)
      {
        this.minInterval = 1000;
        this.doInterval();
      } else if(!this.interval)
      {
        this.doInterval();
      }
      this.listeners[this.listeners.length] = l;
      this.update(l);
    }
   
    /* Timestamp in seconds from 1/1/1970 */
    this.setTimeStamp = function(ts) {
      var d = new Date();
      this.diff = ts * 1000-d.getTime() ;
    }
   
    this.update = function(l)
    {
      var d = new Date();
      if(this.diff) {
        d.setTime(d.getTime() + this.diff);
      }
      this.lcJ = d.getDate();
      this.lcD = this.lcJ < 10 ? "0" + this.lcJ : this.lcJ;
      this.lcW = d.getDay();
      this.lcN = d.getMonth()+1;
      this.lcM = this.lcN < 10 ? "0" + this.lcN : this.lcN;
      this.ucY = d.getFullYear();
      this.lcY = d.getYear();
      this.lcG = ((d.getHours() - 1) % 12) + 1;
      this.ucG = d.getHours();
      this.lcH = this.lcG < 10 ? "0" + this.lcG : this.lcG;
      this.ucH = this.ucG < 10 ? "0" + this.ucG : this.ucG;
      this.lcI = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
      this.lcS = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
      this.lcA = d.getHours() < 12 ? 'am' : 'pm';
      this.ucA = d.getHours() < 12 ? 'AM' : 'PM';
      this.ucF = this.lmonths[this.lcN];
      this.ucM = this.smonths[this.lcN];
      this.ucS = this.Svals[this.lcJ] ? this.Svals[this.lcJ] : 'th';
     
      if(l) {     //Update single Listener
        l.update(this);
      } else {    //Update all Listeners
        for(i = 0; i < this.listeners.length; i++)
          this.listeners[i].update(this);
      }
    }
    this.doInterval = function()
    {
      clearInterval(this.interval)
      this.interval = setInterval('_clockInterval()', this.minInterval);
    }
  }
 
  function _clockInterval()
  {
    if(mainClock)
      if(mainClock.update)
        mainClock.update();
  }
 
  function ClockListener(elemId, phpDateFormat, startTime)
  {
    this.elemId = elemId;
    this.format = phpDateFormat;
    this.update = function(clk)
    {
      if(document.getElementById)
        if(document.getElementById(this.elemId))
        {
          str = this.format;
          if(this.format.indexOf('h') > -1) str = str.replace('h', clk.lcH);
          if(this.format.indexOf('j') > -1) str = str.replace('j', clk.lcJ)
          if(this.format.indexOf('d') > -1) str = str.replace('d', clk.lcD)
          if(this.format.indexOf('w') > -1) str = str.replace('w', clk.lcW)
          if(this.format.indexOf('n') > -1) str = str.replace('n', clk.lcN)
          if(this.format.indexOf('m') > -1) str = str.replace('m', clk.lcM)
          if(this.format.indexOf('Y') > -1) str = str.replace('Y', clk.ucY)
          if(this.format.indexOf('y') > -1) str = str.replace('y', clk.lcY)
          if(this.format.indexOf('g') > -1) str = str.replace('g', clk.lcG)
          if(this.format.indexOf('G') > -1) str = str.replace('G', clk.ucG)
          if(this.format.indexOf('H') > -1) str = str.replace('H', clk.ucH)
          if(this.format.indexOf('i') > -1) str = str.replace('i', clk.lcI)
          if(this.format.indexOf('s') > -1) str = str.replace('s', clk.lcS)
          if(this.format.indexOf('S') > -1) str = str.replace('S', clk.ucS)
          if(this.format.indexOf('a') > -1) str = str.replace('a', clk.lcA)
          if(this.format.indexOf('A') > -1) str = str.replace('A', clk.ucA)
          if(this.format.indexOf('M') > -1) str = str.replace('M', clk.ucM)
          if(this.format.indexOf('F') > -1) str = str.replace('F', clk.ucF)
          document.getElementById(this.elemId).innerHTML = str;
        }
    }
  }

  function loadClocks() {
    // load the Clock
    c = new Clock();
    // Set the clocks date
    c.setTimeStamp(<?php echo mktime(0,0,0,12,12,2006);?>);
    // attach a listener which loads and updates the time
    // in a HTML element with id="myClock", using the
    // dd-mm-yyyy hh:mm format.
    c.addListener(new ClockListener("myClock", "d-m-Y H:i:s"));
  }
</script>
<body onload="loadClocks()">
  <div id="myClock"></div>
</body>
0
 

Author Comment

by:gstalin
ID: 16926068
Hai Roonaan,
Thank you very much for your valuable answer.

Thanks
Bala
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question