JavaScript & PHP‎ > ‎

Cheat Clock

About

A clock that displays the current time for the host server by cheating - the start time is specified using PHP (or similar) and the script adds 1 to the seconds each second and adjusts minutes and hours accordingly. It should be accurate to within a second of the server's time (depending on page loading time).

The idea is that it will always display the time for a certain timezone regardless of where the person viewing the page is located.

A new feature in version Charlie is the ability to have multiple clocks on a page.

Version Delta introduced the ability to display the day. Previously it had to be done from outside the JavaScript.

Version Echo greatly simplified the setup. I try to make new versions backwards compatible, but unfortunately version Echo and onwards are not backwards compatible with the old setup sections.

Example

NOTE: The example is non-functional; once I spend more time with Google Sites I'll hopefully get all the JavaScript working. At the moment my primary goal is availability of the scripts. The example is, however, representative of the appearance of the proper script.

Also note these clock wouldn't do Daylight Savings Time; they'd display the Standard Time for the four cities...

London: Mon 7:52:21 pm GMT

San Francisco: Mon 11:52:21 am PST

New York: Mon 2:52:21 pm EST

Tokyo: Tue 4:52:21 am JST

Using

Clocks are defined in cheatclock.php itself, in the section at the top labelled "Setup Section".

The format for configuring a clock is:

"ID" => array("code" => "TIMEZONE", "offset" => HOURS),

ID is the ID that is to be used in the <span> (explained later), TIMEZONE is the code for the timezone (GMT, PST, EST, etc.), and HOURS is the difference in hours from UTC. The quotation marks are important.

For example, the setup section for the four clocks used in the example above would be:

// SETUP SECTION
$clocks=array(
	"london" => array("code" => "GMT", "offset" => 0),
	"sanfran" => array("code" => "PST", "offset" => -8),	
	"newyork" => array("code" => "EST", "offset" => -5),
	"tokyo" => array("code" => "JST", "offset" => 9),
);
// END OF SETUP SECTION

Once the setup section has been completed, add the following to the <head> section of the webpage(s):

<script type="text/javascript" src="cheatclock.php"></script>

Please Note: If you are upgrading from version Delta or earlier, not only do you need to change to the new setup section, but it is important to note that the filename has also changed from "cheatclock.js" to "cheatclock.php".

Then add: <span id="ID"></span> where you want the clock to appear for each of the specified clocks (where ID is the ID specified in the setup section).

Optional fail-safe

It is not necessary, but it would be a good idea to use PHP to enter a static time into each <span>. That way if the person viewing the page does not have JavaScript they will still see the static time, but if the do have JavaScript then the static time will be replaced by the dynamic time. For example, the code for the New York clock would be:

<b>New York</b>: <span id="newyork"><?php echo date("D g:i:s a", time()-date("Z")+(-5*3600)); ?> EST (Clock not running)</span>

As well as the label, ID, and timezone code, the the only other change for different clocks should be that the "-5" will need to be changed to the UTC offset for the relevant timezones.

Feedback

I'd like to hear what you have to say about this or any other project on the site.

If you have any comments, compliments, or suggestions feel free to get in touch and let me know.

Download

Download the latest version using the link below.

Note that the first version uploaded here was last updated 2008-01-24. The date of any subsequent changes will be accurately reflected in the date shown in the table.

SelectionFile type iconFile nameDescriptionSizeRevisionTimeUser
ċ

Download
A JavaScript clock that displays the current time for the host server by cheating - the start time is specified using PHP (or similar) and the script adds 1 to the seconds each second and adjusts minutes and hours accordingly.  3k v. 4 23 Mar 2013, 21:44 Richard Winskill
Comments