Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

client and server timezone woes

Status
Not open for further replies.

pr0fess0r

Programmer
Jul 10, 2001
25
NZ
Hi

I am trying to bypass the whole issue with a server being in one timezone and clients in the other by being a bit clever. Having the user choose a tiemzone froma dropdown isnt working for me - daylight savings etc always seems to screw the time up, so what I am doing is passing a hidden form variable when the user logs in that holds their local time (using javascript) and then the difference betwen their local time and the PHP server time becomes their time offset, added to every time they view from the database.
So
Login form has:
<input type=hidden name=clienttime>
and
<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
var now = new Date();
var epoch_seconds=Math.round(now.getTime()/1000);
document.form.clienttime.value=epoch_seconds;
//--></SCRIPT>

and I write a session variable called SESSION_TIMEOFFSET which is $clienttime-time();

This works fine on my local network, even when I change the server time. On our live environment (i'm in New Zealand, server is in USA), both clienttime and time() are the same! They're both the US server time! Deosnt javascript retrieve time from the local client machine? WHat am I missing here?

cheers

Lucas Young
 
Since the JavaScript object method Date.getTime() returns the date in GMT, it could be that the server has been set up to use GMT internally.

You might try:

print date ('Z');

to see what the server's GMT offset is.

Want the best answers? Ask the best questions: TANSTAAFL!
 
But doesnt Date.getTime() run on the client machine, and return the client's time? My machine is set to NZT i.e. New Zealand Time, and the server is in Florida...
 
Yes but my question is, where is it getting the time from? The web server or the client's machine? The code I posted seems to return identical values for both time() (PHP) and epoch_seconds (Javascript) yet they should be different because Javascript is getting the time from my machine and php gets it from the server, which is ina different country
 
But what sleipnir214 is saying is that if the server is set to GMT, and your JS code (which runs on the client machine) fetches the time in GMT, there would be no difference.

//Daniel
 
Ah OK, then the answer is no, because when I convert the server time from a unix timestamp to human readable form (<?=date(&quot;F j Y, g:i a&quot;)?>) I get May 5 2003, 4:05 am and this javascript:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
var now = new Date();
var epoch_seconds=Math.round(now.getTime()/1000);
document.write(epoch_seconds);
//--></SCRIPT>
returns May 5 2003, 4:05 am as well, and it's 8:05pm here :)
 
prOfessOr,

Use deliberate UTC time functions in JavaScript:
Code:
var time = new Date(); 
var hour = time.getUTCHours(); 
var minute = time.getUTCMinutes(); 
var day = time.getUTCDay();
 
OK thanks, how would I get UTC epoch seconds? or at least get the difference between my local time and the server time in hours?
 
I read your original post more careful this time.
You do want the local time of the client, so you can use the timezoneoffset:
getTimezoneOffset

Description
Gets the difference, in minutes, between the computer's local time and universal time

Syntax
date1.getTimezoneOffset()

Parameters
None

Returns
An integer (hours)

In conjunction with the UTC time that should help you out.
 
OK, so this will return the difference between the client machine's time and UTC/GMT. So, in order to calculate the difference between the server's time and the client's time, I need to get the offset between the server's time and UTC as well and then take the difference, is that right?
 
Code:
function makelocaltime() {
	var today = new Date();
	var difftime = (today.getTimezoneOffset()+420) *60000;
	today.setTime(today.getTime()+difftime);
        // set the value to a hidden field	
        document.form.localtime.value = today.toLocaleString());
       // or in UNIX
       document.form.unixtime.value = (today.parse)/1000;
}
Suggestion:
1. Have JavaScript generate the local time. If you want seconds in UNIX format use the date.parse (this method returns the number of milliseconds since January 01 1970 00:00:00) and convert it to seconds.

2. On posting calcualte the difference.

;)
 
Thanks Boss, I'll give that a try
By the way, whats the +420 for?
 
Hmm that didnt work
<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
var today = new Date();
var difftime = (today.getTimezoneOffset()+420) *60000;
today.setTime(today.getTime()+difftime);
document.form.clienttime.value=(today.parse)/1000;
//--></SCRIPT>

Gives me NaN for the value of clienttime on the next page....
 
although it does work on our local development environment...
 
prOfessOr,

I suggest that you just pass the GMT offset in a hidden field.

I think the inherent trouble is that any UNIX timestamp used refers to GMT and any localization is lost when conversion happens.

1. Capture dateObj.getTimezoneOffset() yields minutes between local time and GMT
2. You know your server's timezone and can turn that into a constant.

I made a mistake with Date.parse(dateObj); the documentation wasn't clear, but this is the way the syntax should be. It returns milliseconds since 1/1/1970 00:00:00 in GMT, so it won't help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top