2

Hello I'm coding a chatting box and I'd like to stamp the current local time of the user next to its posted message. So I could use javascript :

var date = new Date();

and send a string form of the variable to the server script

but what if the Javascript is not activated ?

I know it is a really low percentage of the web users but I really want to know if there is a way (on the server side) to retrieve the local date of the user in its HTTP request :

something like $_SERVER['local_date'] would have been great ? (I know it doesn't exist but anyway is there a similar way ?)

vdegenne
  • 12,272
  • 14
  • 80
  • 106
  • 3
    I would recommend forcing javascript on your users – Rene Pot Dec 06 '11 at 10:10
  • 1
    @Topener is correct. There's no variable like the one you are looking for. You can get the server's current time using php, and js can get the local time (if activated). As far as I know, there's nothing in between. Anyways, how many have disabled js today? – OptimusCrime Dec 06 '11 at 10:12

3 Answers3

2

The only way I can think of is by analyzing the client IP address and extract the county from it, then you can calculate the time difference.

However, while it may work for many, it's far from being reliable and some countries have several time zones so it's pretty much far fetched.

I would advice to simply put message like "To use this chat, please enable JavaScript" inside <noscript> tags.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

You could ask the users of your system to specify their timezone, though of course that depends on the user actually setting their timezone for you correctly.

GordonM
  • 31,179
  • 15
  • 87
  • 129
0

Is there anyway to get client-side time other than JS? No.

However, this isn't how the traditional implementation works anyway.

Most likely, you're not going to use the client-side time, but rather the client-side time offset.

The traditional implementation stores messages along with the corresponding server-side timestamp in the database (what time it was on the server when the message was received).

Upon being displayed, it is then adjusted to client time by adding a client-time offset.

Client-time offsets can be determined in a few different ways:

  • Javascript
  • Default to some timezone and allow the user to specify a different one (this version is popular with forums where you have to register an account).

Once you've established the offset, the adjustment can be done in two places:

  • Either in the server-side application (PHP, etc.) so that the client-side times are written to the page.
  • In Javascript

So, the answers provided so far are correct, but I just want to explain how its traditionally done.

jedwards
  • 29,432
  • 3
  • 65
  • 92