0

I have a simple site under development where users can question & answer. The problem that I face is that there are users from many timezones across the globe who might be posting where their respective time will be displayed as well.Someone is posting from Asia/Calcutta,someone else from America/Phoenix and some other from Europe/London etc etc. For example, I have set the default timezone:

date_default_timezone_set('Asia/Calcutta');

People from that timezone will see the exact time.But for the people from America/Phoenix or Europe/London I have to set :

date_default_timezone_set('America/Phoenix');

or

date_default_timezone_set('Europe/London');

But all this is in a static way that I use only to explain. In real scenario how do I accomplish this task as I don't know which user from which timezone is posting.So setting timezone to specific timezones will be useless in this case. How do I resolve it dynamically or any other means of doing this?

Passerby
  • 9,715
  • 2
  • 33
  • 50
Parveez Ahmed
  • 1,325
  • 4
  • 17
  • 28
  • Here's another stackoverflow that discusses one (good) way to detect and retain timezone information that's appropriate for each user http://stackoverflow.com/a/7696334/2119660 – Jeff Sisson Nov 14 '13 at 02:56
  • You can use UTC at your server, save every time in database as UTC, and use client side JavaScript to display local time according to client side timezone. – Passerby Nov 14 '13 at 03:05
  • @Passerby yeah that is a good idea that i have already given a thought myself. But there is a problem associated with it. Suppose, the default timezone in my server is UTC. When a user posts at,say 9.52 am (client time),but it will be considered as UTC time at the server and when the same user views it back, it is converted into the local time in his own timezone,then he won't get 9.52 am as he posted before,but it will be something computed from UTC 9.52 am,right? – Parveez Ahmed Nov 14 '13 at 04:26
  • 1
    @rosemary No, you don't use time from client, you use time at your server. Whenever _your server receive a post_, you store with the time of your server. You send back the UTC timestamp to your client, and use JS to calculate it back to client's local time. – Passerby Nov 14 '13 at 04:35
  • @Passerby Thanks. Excellent! At the client side I simply have to format the timestamp just as we do in PHP,right? Which functios come in handy in javascript for doing this as we use date() function in PHP. Thanks agin! – Parveez Ahmed Nov 14 '13 at 04:56
  • @rosemary Actually, [`Date` constructor in JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) accepts UTC timestamp as parameter (in milliseconds, though). – Passerby Nov 14 '13 at 05:25

0 Answers0