0

I am making an app where user can publish an article. If somebody in Japan publishes an article on 2017/03/25 09:00 should I set the articles published date and time to that time or should I set it to servers time?

Also if somebody in USA enters the website and is about to read the article, what date should the article say it was published?

If I say 2017/03/25 09:00 that time has not happend yet for the user in the USA and it would be in future for him, what time should I show to somebody from the other end of the world?

niko craft
  • 2,893
  • 5
  • 38
  • 67
  • Save the time in UTC timezone, and display it to the users based of their time zone's offset from UTC – Alon Eitan Mar 25 '17 at 20:16
  • Hi I plan to use UTC timezone when saving to database, but I a wondering what will someone in other part of the world see when time published has not yet happend for them? – niko craft Mar 25 '17 at 20:18
  • So if they are UTC+2 and you're UTC-5 you'll see the time minus 7 hours (Or am I missing something?) – Alon Eitan Mar 25 '17 at 20:25
  • thanks I got it now – niko craft Mar 25 '17 at 20:31
  • a follow up question, if user is not logged in my application and I am presenting a list of articles to a visitor on his first visit, what should I show the visitor that I do not know timezone of? I can't use javascript which has functions to extract timezone since this is his first visit and javascript executing first time and sending data about timezone to server will work for second visit as long as session lasts and then same problem when session is lost. What to do? – niko craft Mar 25 '17 at 21:58
  • On the client side you need to construct a date object from a UTC time (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC) So you just need to make sure that the tiem return from the server is UTC and you're good to go. If you're outputting the time on the server side, you might want to read this http://stackoverflow.com/questions/409999/getting-the-location-from-an-ip-address – Alon Eitan Mar 25 '17 at 22:31

1 Answers1

0

As I see it, the more consistent way of dealing with time is to only use UTC time (very similar to GMT in practice, besides, php gmt* function actually are using UTC time) internally, eg date/time stored in persistent storage (db etc) and to use user timezone to display time to each user.

This way, you keep chronology, simplify design and thinking and always display a time that is meaningful to the user, eg, the time in his/here timezone.

In your example, it would not matter what time to display, as you would be displaying the only relevant time, eg, the time it was actually created UTC, just translated to each user's timezone. And don't worry, time is always flowing, no one can see what did not occur as long as the time reference is consistent. It does not matter if someone sees a different time on his clock, it still can be the same moment.

If you where to deal with guests, you could perform educated guesses about the proper timezone using geolocation and / or browser prefs. And you can still allow timezone switch in your views.

It's also a way to simplify your model as using one single time reference will ease time based comparison across objects and will lower the risk of miss-configuration (one single time setup everywhere).

Carbon is easy to use with laravel, and is a convenient way of working with date/times.

fab2s
  • 838
  • 2
  • 8
  • 14
  • thanks I plan using UTC as laravel is preconfigured with that, I am just confused what is displayed to the user who visits the blog, so you suggest I display the UTC time as time published and not the time for example in japan where the creator of the post was, this is so confusing for me. Even if I display UTC time, what should someone who is behind UTC time see, for them it will be time in future, or am I thinking wrong? – niko craft Mar 25 '17 at 20:21
  • No I suggest you just use UTC internally, and then, in your view, display that exact time translated to user's timezone. It's the same thing as saying it's 6h am in NYC and noon in Paris. The two occurs at the same actual time, even though clocks are not showing the same digit. What you display to your user is the clock, the time is what is stored in your db. There are function to do exactly that in php eg display time from this timezone in this other one, so it's trivial. – fab2s Mar 25 '17 at 20:25
  • a follow up question, if user is not logged in my application and I am presenting a list of articles to a visitor on his first visit, what should I show the visitor that I do not know timezone of? I can't use javascript which has functions to extract timezone since this is his first visit and javascript executing first time and sending data about timezone to server will work for second visit as long as session lasts and then same problem when session is lost. What to do? – niko craft Mar 25 '17 at 21:57
  • You can do two things as I mentioned in my answer : use ip based geolocalization and / or browser language detection to guess the most relevant timezone to use, and display the timezone used to generate date in your views. Ideally, you could add a button for unregistered users to switch timezone if the guess was not enough and it matters to them. Browser prop detection is easy in javascript, geoloc requires some ajax roundtrip, but it can be cached in locastorage or cookies. – fab2s Apr 02 '17 at 18:44
  • thanks for your advices, I have found a jquery library that converts UTC time to local time if I output it in my html and put it in date tag, so I use that if user is not logged in, the simplest solution to me – niko craft Apr 02 '17 at 21:05