display date/time on website base on user computer time in different country

落爺英雄遲暮 提交于 2019-12-11 06:39:20

问题


I am developing a chatting messenger of website application. For my concept, I avoid to use timezone.

When a user post a message, I use php to get my current server date/time and save it onto database.

To display the time of all messages to the user, I just need find out the different between server time and user's system time. Let's say the different is 3 hours, i just need use mysql to retrieve recorded server time from database then add it with 3, eg : $message-time-display = $server-time-of-message-posted + $different.

For this solution, although the user's computer time may be incorrect, it will not effect the time records that saved on my database. And it will display the correct time when the users have corrected their computer's time. Is this solution is reliable? If so, why there is none of people doing this? Why so many people are using GMT or timezone thingy? Maybe I miss some important issue?


回答1:


One quick fix is not to show the exact time, rather the time elapsed. Ex: 2 minutes ago

And then...

Since you know the elapsed time you can substract that from the user's current time. You can get the user current time with javascript.

var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();



回答2:


If you are writing an app that will only ever be on one server then it's ok to store time as your local server time. If your application will be deployed in many locations around the globe, then it makes better sense to store all times as UTC.

To my mind, user-provided times should be translated from user's timezone to UTC (or your server's timezone if it's your personal app) before being stored and should be translated back to the user's timezone when displayed.

So in your case, it should go something like this:

  1. user action -> store UTC time in database
  2. another user action -> compare current UTC time to stored UTC time and display difference.

If you're displaying timestamps, it should go like this:

  1. user action -> store UTC time in database
  2. user access -> retrieve stored UTC time and translate it to user's timezone. Display.



回答3:


Your solution should work as long as the user has the time set correctly. That is the only problem actually. Why do you think no one is doing it ? I guess most of them do and only the lazy ones relies on GMT/EST times.

Hint: I saw the user time sent in tracking codes such as google's analytics javascript, probably for computing the difference you are taking about ;)



来源:https://stackoverflow.com/questions/5600117/display-date-time-on-website-base-on-user-computer-time-in-different-country

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!