What's My Time?

This PHP/Javascript combination will, with any luck, go some way to replacing all of those ridiculously pointless Javascript clocks that simply display the current user's time!

Here's the time and date where I am: 00:00:00 - Wednesday 09 July 2008

Place the Javascript and PHP in the <head> of the html document.

PHP

<?
  // $gmtoffset : your timezone. For example, UK is 0, Colorado is -7, Germany is +1, etc.
  $gmtoffset = 0;

  // $dst : Daylight Saving Time zone. This varies around the world - see http://webexhibits.org/daylightsaving/b.html and http://webexhibits.org/daylightsaving/g.html for more information.
  //        At the moment, only 3 values are calculated:
  //                0 -- no Daylight Saving Time
  //                1 -- European Union DST
  //                2 -- United States DST
  $dst = 1;
?>

Javascript/PHP

var servergmtime = new Date("<? echo gmdate("M d Y G:i:s"); ?>");
var timecorrection = (new Date() - servergmtime);
var dst = <? echo $dst; ?>;
var gmtOffset = (<? echo $gmtoffset; ?> * 3600000);

timeID = window.setTimeout("timeUpdate();", 10);

function timeUpdate() {
  var weekday = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

  //Get current date
  timeNow1 = new Date();

  //Apply the correction
  var expdate = timeNow1.getTime();
  expdate -= timecorrection;
  timeNow1.setTime(expdate);

  if (dst != 0) {
  	dstinfo=new WhatDates(dst,timeNow1.getFullYear());

	with (dstinfo) {
  		currentYear = timeNow1.getFullYear();

  		var startDST = new Date(currentYear,dstMonth1-1,dstMonth1Date,1,0);
  		var endDST   = new Date(currentYear,dstMonth2-1,dstMonth2Date,1,0);

  		if ((timeNow1.getTime() > startDST.getTime()) && (timeNow1.getTime() < endDST.getTime())) {
			var timeNowMs = timeNow1.getTime();
			timeNowMs = timeNowMs + 3600000;
			timeNow1.setTime(timeNowMs);
  		}
	}
  }

  var timeNowGMT = timeNow1.getTime();
  timeNowGMT = timeNowGMT + (gmtOffset);
  timeNow1.setTime(timeNowGMT);

  hours = timeNow1.getHours();
  minutes = timeNow1.getMinutes();
  if (minutes < 10) {minutes = "0" + minutes};

  seconds = timeNow1.getSeconds();

  displayTime = hours+":"+minutes+":";
  if (seconds < 10) {seconds = "0" + seconds};
  displayTime+=seconds+" - "+weekday[timeNow1.getDay()]+" "+timeNow1.getDate()+" "+monthname[timeNow1.getMonth()]+" "+timeNow1.getFullYear();

  if (document.all) {
    //IE or Opera
    document.all['showTime'].innerHTML = displayTime;
  } else {
    //Mozilla
    document.getElementById("showTime").innerHTML = displayTime;
  }
  timeID=window.setTimeout("timeUpdate();",1000);
}

function WhatDates(dsavingt,thisYear) {
  var dsavingt, thisYear, AprilDate, OctoberDate, MarchDateEU, OctoberDateEU;
  thisYear = Math.round(parseInt(thisYear));

  // European Union
  if (dsavingt == 1) {
  	this.dstMonth1 = 3;
  	this.dstMonth2 = 10;
  	this.dstTime = 1;
  	this.dstMonth1Date = (31-( Math.floor (thisYear * 5 / 4) + 4) % 7);
  	this.dstMonth2Date = (31-( Math.floor (thisYear * 5 / 4) + 1) % 7);
  }

  // USA
  if (dsavingt == 2) {
  	this.dstMonth1 = 4;
  	this.dstMonth2 = 10;
  	this.dstTime = 2;
  	this.dstMonth1Date = (2+6 * thisYear - Math.floor (thisYear / 4) ) % 7 + 1;
  	this.dstMonth2Date = (31-( Math.floor (thisYear * 5 / 4) + 1) % 7);
  }

}

HTML/PHP

...and place the html wherever you want the clock to appear!

<span id="showTime"><? echo "00:00:00 - ".gmdate("l d F Y");?></span>