Different timezones and UTC in database

2 years ago

SvvimX
Member | 65
+
0
-

Hey guys,

I'm using Nette with Kdyby/Doctrine. I need to store datetimes and to show them to the users in their timezone. What is the best way, how to do that? I mean, is there an easy way how to convince doctrine to convert datetime into UTC before writing it into database and how to easily obtain user timezone to render the datetime (using latte).

Thanks!

2 years ago

srigi
Member | 560
+
+1
-

Hi, you must do couple of things to correctly display time in your app:

1. setup timezone of the server/application
Set this line to correct value by the geolocation of the server. If your server is in Frankfurt for example, set it to Europe/Berlin. If you are the administrator of the server, install/setup NTP daemon too, so the server clocks are always at correct value.

2. allow user to set their timezone
you must provide user with some sort of timezone setup. Screen like this:

You must create this list of all possible timezones (use that wiki page link above).

3. store all date-time events/data as timestamp
Timestamp is best when dealing with timezones, because timestamp is timezone independent. You can convert timestamp into timezone time with code below.

4. display date-time event/date in users' timezone

$userTimezone = 'Asia/Ashgabat';  // get this from DB, step 2
$postCreatedAt = 1495971234;      // timestamp of date-time event you want to display, step 3

$datetimeObj = new Nette\Utils\DateTime();
$datetimeObj->setTimestamp($postCreatedAt);
$datetimeObj->setTimezone(new DateTimeZone($userTimezone));

echo $datetimeObj->format('Y-m-d H:i:sP') . "\n";
// 2017-05-28 16:33:54+05:00

Last edited by srigi (2017-05-28 13:47)

2 years ago

SvvimX
Member | 65
+
0
-

srigi, thanks.

  1. ok, done already
  2. Do I have to allow the user to edit the timezone? I would prefer to obtain it auto-magicaly from the user request.
  3. ok, I am planning to use UTC DateTime, but it should be the same
  4. ok, so I have to edit all latte templates and server site work with date

2 years ago

srigi
Member | 560
+
0
-

2. yes, users must setup their tz by themselfs. There is no timezone data in the request. However, you can use IP address to geolocate user request, usage.
3. ok
4. don't forget to set timezone of the datetime object to the users' tz.

Last edited by srigi (2017-05-28 15:45)

2 years ago

SvvimX
Member | 65
+
0
-

I did a little hack.

  1. I set up the server timezone to UTC – now all datetimes stored in database as well as all datetimes created via new \DateTime are concidered as UTC
  2. I've used https://bitbucket.org/…mezonedetect which will determine the timezone on the client side and sends it to me on server (first time only for the current session)
  3. I only have to update all usage to set the obtained timezone

If this won't be enough for client, we'll add the timezone settings to the registration form (but even then, we have to decide the timezone for non registered users)…