Server DateTime dispaly PHP

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
gstalinAsked:
Who is Participating?
 
RoonaanCommented:
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
 
RoonaanCommented:
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
 
gstalinAuthor Commented:
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
 
gstalinAuthor Commented:
Hai Roonaan,
Thank you very much for your valuable answer.

Thanks
Bala
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.