views:

49

answers:

4

Since the Date constructor is based on your PC's timezone and server-side is obviously dependent on the server's time settings...

What procedures can I do to ensure that the timestamps I generate are consistent on both ends?

I have a jQuery datepicker setup and I do a lot of work, I also have my own datepicker that's generated from server-side code which looks up db availability for bookings.

I'm saving arrival/departure times on the server-side and the days are off by one because the JS is local timezone ( mine is Eastern ) .

Is my only option doing something like making the JS call PHP to grab a timestamp?

+2  A: 

You need to use one time reference (typically UTC/GMT) see this.

http://articles.techrepublic.com.com/5100-10878_11-6016329.html

renick
+3  A: 

You could have your client query the server once at startup to get the server's time, then calculate the difference between the time on the client and the time reported by the server. Cache this value, and use it for future requests. You may still be off by a few seconds (owing to the request latency), but you'll be pretty close.

Curtis
When you mention future requests, does that mean save the difference in a session/cookies?
meder
You could store it in a cookie, but you'll want to make sure that it expires after a few minutes, I guess. I was actually thinking that your JS could store it in a local variable that exists for the life of the page - if the user refreshes that page, it's probably safer to re-check the time difference.
Curtis
+1  A: 

Your server and client application should get the time from a remote server (timeserver)

Thau
+1  A: 

You only need sync the server time from a time server. Evry time your application is working in the client side read this sync-ed time from a reference table.

P.j.Valle