Solved

Server DateTime dispaly PHP

Posted on 2006-06-16
4
308 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql between clause 2 22
echo paypal data on screen 5 48
Showing random records from database 10 36
PHP not parsing ' character 12 38
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 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