W
W
WebDev2020-04-12 14:09:49
PHP
WebDev, 2020-04-12 14:09:49

How to show the user data in his time zone and is it necessary?

The time on the server is in UTC. For all entities, the creation time is measured in timestamp and transmitted to the user in the browser as a timestamp, while the user on the computer is already converted to date and time. Thus, each user sees the date and time according to their time zone.
But there is also the functionality of charts for 7 days.

select count(*) as total, date
from events
where events.created_at > interval - 1 week
group by date

And here I have 2 questions:
1) The server generates a list for 7 days according to its server time. The user has 2 am on April 12, and the server still has 23:00 on April 11. How, in this case, to show data taking into account the F.P. user, because they are formed on the server?
2) The second question is whether this is correct and necessary? Is it correct for each user to show graphs in his time zone, or is it more understandable and better to show the graph and write that the data is in UTC, and let the user calculate it himself?

To solve the first question, I have only one option in my head. The browser has Date.getTimezoneOffset which returns the number of minutes from UTC. This value is sent to the server and summarized before fetching.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor Aristov, 2020-04-12
@Egor3f

1. Sign the date on the charts in the user's time zone. If they are shown exactly for the last N days, then such nuances will be invisible to the end user anyway. If the user selects an arbitrary interval, then already convert.
I advise you to choose one time storage format and use it to the maximum in order to reduce the number of places where the conversion is performed
. Also, move the conversion itself into a separate function, even if it is used in 1 place - in the future it will be easier to change
2. Depends on the nature of the data and tasks, according to what is described in the question, it is impossible to say. Here the question is more to the subject area

H
hint000, 2020-04-12
@hint000

The answer is highly dependent on the subject area. If this is the MCC, then no time zones, even Baikonur, even East, even Cape Canaveral - for all UTC. If this is a train schedule, then before the whole country was poisoned and arrived according to Moscow time, but then they decided to do it according to the local time (one can argue about the correctness of the decision, but for foreign passengers it is more logical to see the local time). If you can't decide which option is best, then give users a choice. This is where UI/UX science comes into play, so that choices help the user, not confuse them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question