V
V
Vitaly Voskobovich2016-03-13 11:24:39
PostgreSQL
Vitaly Voskobovich, 2016-03-13 11:24:39

PHP, Yii2 and PostgreSQL. How to make support for time zones for users?

I have a typical task - to make an application with support for time zones (so that the user chooses his time zone in the settings).
This will be used for the dates of creation / updating of records in the database, the time of the user's login, the time of fixing the log.
Studying the topic, I learned that for my task the correct option would be to store the date in UTC, and on the client, when recording and displaying, transfer to the user's time zone.
But here there are several nuances:
1. What about the transitions to winter / summer time?
After all, a date saved a year ago in UTC may not be displayed correctly today.
I did not find comprehensive information on this issue and did not fully understand it.
I read about the tzdata database, which helps to correctly convert past dates, but again I did not understand how to use it. Is it really necessary to write the logic of working with it yourself?
2. They write that PostgreSQL is good at working with timezones.
That is, we create the type timestamp with timezone and the base does everything for me.
But here again there is no understanding.
2.1 How does the database recognize the client's time zone? Probably it is necessary to send request like SET SESSION TimeZone TO -5; at each connection to base, a crutch any.
2.2 The date format that the user will see is the display level, and I'm trying to solve it at the data level, or even business logic. I don't think this is correct.
3. If the implementation of the translation of Timezone => UTC and vice versa is done when displaying, then how is this implemented in the code?
In which timezone to initialize the application (date_default_timezone_set) ?
How to translate time and at what point in time?
Write answers to questions and your recipes for solutions.
Any information would be helpful to me.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Voskobovich, 2016-03-13
@voskobovich

Here are the thesis answers to my question
1. You need to store dates in UTC in the
database 2. Yii2 Formatter is able to take into account the time zone out of the box **when outputting**. You set up a time zone, sends time in UTC to it, and it will do everything for you.
3. Yii2 Date Validator can out of the box **when writing** a date to a model attribute, convert it back to UTC and pass it to the model attribute specified in the validator settings in order to further operate with data again in UTC.
4. After logging in, the user needs to change the formatter's timeZone to the one specified in the profile of the logged in user. While the user is not logged in, we work in UTC.
5. In UTC, you need to translate the OS, database and PHP. Accordingly
date_default_timezone_set('UTC');or in php.ini
6. It is better not to shift the time format to the base, since moving to another base or taking data from the cache will break everything.

A
Anton Natarov, 2016-03-13
@HanDroid

I don't know how to do this in pure PHP, but in Yii 2 it's enough to do this.

'components' => [
    'formatter' => [
       'dateFormat' => 'd.MM.Y',
       'timeFormat' => 'H:mm:ss',
       'datetimeFormat' => 'd.MM.Y H:mm',
   ],
],

Time and date output
/* Тут идет установка часового пояса
* естественно вы можете хранить их в кэше или БД 
* или вычислять по IP или JS. Тайм зоны можно хранить 
* в разных форматах. Лучше в UTC, а еще лучше в
* UNIX timestamp. 1412599260 / 2014-10-06 12:41:00
*/

$myTimeZone = "Europe/Berlin"; 

Yii::$app->timeZone = $myTimeZone;
Yii::$app->getFormatter()->asDate(time()); // время 
Yii::$app->getFormatter()->asDatetime(time()); // Дата и время

If memory serves, then you can work with the time zone in this way. Naturally, you can make a widget and post where you need.
List of timezones available in PHP
UPD: Regarding daylight saving time. It is possible to fasten the "take into account daylight saving time" checkbox in the profile form, and then make a trigger in the database that would change +1 and -1 hour timestamps on a certain date.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question