ServerDate to JavaScript
(A free script from MRE Software)

 
My server time: 
Your system: 
 

I have a page on a website with a calendar JavaScript which posts upcoming dates.  I wanted to control the initialization date on this script from the server as opposed to using the client computer's local date.  I went looking around on the web for a "quick fix" but what I found involved either renaming the page with a php or shtml extension and using php or a server side include.  Since this just happened to be the long established home page to my site, I didn't want to change the name.  After some thought, I came up with a solution to the problem.  Since I have used so many other scripts and ideas from the web, I thought I would give one back.

A little more explanation... This time business is a "can of worms".   What this actually does is set a JavaScript variable, on the client's machine, to the actual time on the webserver.  This variable is expressed as a local time. Confused?  Maybe an example will help. Example:  It is 07:21:00 on my server located in lets say Chicago (central time).  A user in San Francisco (pacific time) brings up my website.  When this routine is called, the variable servertimeOBJ will return the time in Chicago (+2 hours ahead of SF)  ServertimeOBJ will be 07:21:00 with the appropriate date etc. as required.  If I use servertimeOBJ to adjust my scripts, the SF user will see dates/times in Chicago.  Lets say I have a clock on the site...When the SF user pulls up the site the clock will display 07:21:00 to him even though it is 05:21:00 at his location.  He sees Chicago time.  This script can be edited, modified, adjusted, whatever to suit your exact needs, hopefully it demonstrates some useful concepts.

First of all you will need to be able to run CGI programs on your server as you can't query the date from it without this ability.  In my case I used a small PHP program to get the date and pass it back to the page via a JavaScript variable.

Once you have servertime.php installed on your site, you will need to call it from your html page.  The example included in the PHP code above is all you need to do.  Of course you should replace "yoursite.com" with the URL of your website.  Call this BEFORE you call any other JavaScripts that need the date/time variable.

In the example below I have a complete html page which calls the servertime.php file in the head section and then runs a JavaScript which uses it print the time on the server.  You can take this basic code and incorporate it into your own scripts.

 

 
The date and time in red at the top of this page was produced using the methods detailed herein.  If you change your local computer date by say two months, change the time zone, daylight time, whatever, so that it is wrong, and refresh this page you will see that the red date/time remains the same and displays the date and time from the server, not from your local system.  You can view the source code of this page for more details... This is a standard html page and uses no Server Side Includes.  To download the JavaScript code that uses the servertimeOBJ variable  and actually displays the ticking times at the top of this page, click here.
 
USE AT YOUR OWN RISK!

Questions / Comments?  Email me.

Submitted in 2005