[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Global Variable

Posted on 2006-05-18
6
Medium Priority
?
193 Views
Last Modified: 2010-08-05
Hi guys,

is there a pre-defined variable for the time and date?

 + any hint's or ideas to impress on my first website?
0
Comment
Question by:foster30
  • 2
  • 2
  • 2
6 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16715477
echo date('Y/m/d');

$nowTimestamp = mktime();

and many more functions which return current date when you omit parameters.

-r-
0
 
LVL 5

Expert Comment

by:aminerd
ID: 16715504
time() also gives the current date as a unix timestamp.

date('H:i:s') gives Hours:Minutes:Seconds
0
 
LVL 5

Expert Comment

by:aminerd
ID: 16715505
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:foster30
ID: 16715567
Can this make the time dynamic?

I want if possible for it to either follow the system clock, or refresh, is that possible?
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 1000 total points
ID: 16715604
Hi,

Put this in a file called clock.js:

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.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();
      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];
     
      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('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;
        }
    }
  }

Then use this html in your page:
<html>
  <head>
    <script type="text/javascript" src="clock.js"></script>
  </head>
  <body>
   <div id="myClock">Loading clock..</div>  
   .. other html

   <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>
  </body>
</html>

-r-
0
 

Author Comment

by:foster30
ID: 16715654
Amazing!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

829 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