ServerDate to JavaScript
Version 2.1
(Another donateWare script from MRE Software)

 

My server time:  
Your system:  

xxx
xxx

 

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 PHP or other server side programs on your server as you can't query the date from it without this ability.  In my case I used the small PHP program below to get the date and pass it back to the page via a JavaScript variable.  You will need to set the default time zone for your server inside the script variable $myTimeZone.  Version 2.0 of this script uses the newer PHP time zone identifiers.  A complete list of time zones can be found here: http://www.php.net/manual/en/timezones.php

Here are some common time zones for the USA.

Atlantic => America/Puerto_Rico (no DST)
Eastern => America/New_York
Central => America/Chicago
Mountain => America/Denver
Pacific => America/Los_Angeles
Alaska => America/Juneau
Hawaii  =>  Pacific/Honolulu (no DST)
Arizona => America/Phoenix (no DST)


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 to display the date+time on the server.  You can take this basic code and incorporate it into your own scripts.


 
Just for fun, mouse over the time display in this example of the script above to see a twist. Server time =  getting server time ...
This was done with OnMouseOver/OnMouseOut events using html code like the sample below:
<span id="sTime" OnMouseOver="displaySeconds=true;textdates();" OnMouseOut="displaySeconds=false;">getting server time ...</span>

 
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,


 

If you find this code of value, donate a little to the cause.  Think of the time you just saved!

USE AT YOUR OWN RISK!

Questions / Comments?  Email me.

MRE Logo