A
A
AlexKuznec2016-06-10 21:20:27
Yii
AlexKuznec, 2016-06-10 21:20:27

How to find the user's time zone in Yii2?

I use in Yii2 DateTimePicker for user to select date/time.
By default, Yii2 assumes that all received times are in UTC.
DateTimePicker when you click on the "Today" button selects the current local time and passes it to the database, where it is perceived as UTC.
I decided to store 2 fields with the specified time in the table at once:
time - int(11) - timestamp of the event in UTC (for calculations)
usertime - datetime - local time record so that the user sees the same time that he saved, even being in a different time zone (for example, on vacation)
Question: how to find out the user's time zone, or at least the time difference between UTC and the user's time?
For some reason, I did not find built-in solutions. Only one dater / dater library, which is not related to Yii2 and strongly duplicates the Formatter functionality, but has the $timezoneDetector->getHtmlJsCode() tool, which determines the time zone through JavaScript with some tricky calculations. But it feels like a crutch.
In most discussions, it is proposed to determine the TimeZone almost independently by writing calculations in JavaScript based on the user's time (here you can make bugs and debug for a long time). Another option is to determine the geographic location by IP.
Are there any ways using Yii2? After all, it supports time zones and the formatter can parse time with the time zone specified, from the documentation:
Yii::$app->formatter->asTime('2014-10-06 14:41:00 CEST');
But I did not find any widgets that can attach a time zone indication to the transmitted data.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AlexKuznec, 2016-06-16
@AlexKuznec

In general, at the moment I found 2 more or less ready-made solutions:
1) Geolocation by IP.
Pros: implemented for Yii2, you can find out the country, region and city of the user, which, in addition to the time zone, gives us information about the expected language and currency of the user. Although, browsers collect the user's language preferences, so it's best to use them if available.
Cons: the complexity of the system, does not guarantee the correct result, there is a small fee for high loads (easily paid off by advertising on the site).
https://github.com/marciocamello/yii2-sypexgeo/
2) Determining the time zone via JavaScript on the client side.
Pros: simple, free.
Cons: no implementations for Yii2 found, works only with correct time settings for the user and an up-to-date database of time zones (I, for example, display outdated data for Barnaul).
I took this library:
https://github.com/barbushin/dater
and built it into Yii2 as follows (if you suggest a better option, I will be grateful):
in the configuration:
'components' => [
'timezoneDetector' => [
'class ' => 'Dater\TimezoneDetector',
]
],
'on ' . yii\base\Application::EVENT_BEFORE_REQUEST => function ($event) {
$clientTimezone = Yii::$app->timezoneDetector->getClientTimezone();
if (isset($clientTimezone)) Yii::$app->timeZone = $clientTimezone;
},
and in template layouts/main.php
< head>
...
<?= Yii::$app->timezoneDetector->getHtmlJsCode() // detect user's time zone ?>
...
< /head>
insert into template I don't like the application of additional code, because I wanted to write a behavior for Yii2, but the following code did not work for me (I tried to hook it to different events)
'on ' . yii\base\Application::EVENT_AFTER_REQUEST => function ($event) {
Yii::$app->view->registerJs(Yii::$app->timezoneDetector->getHtmlJsCode(), yii\web\View::POS_HEAD );
}

D
dmirogin, 2016-06-10
@dmirogin

For some reason, I did not find built-in solutions. Only one dater / dater library, which is not related to Yii2 and strongly duplicates the Formatter functionality, but has the $timezoneDetector->getHtmlJsCode() tool, which determines the time zone through JavaScript with some tricky calculations. But it feels like a crutch.
In most discussions, it is proposed to determine the TimeZone almost independently by writing calculations in JavaScript based on the user's time (here you can make bugs and debug for a long time). Another option is to determine the geographic location by IP.

Yes, only this way.
On the server, you can only get the server's time zone.

M
Maxim Timofeev, 2016-06-11
@webinar

To get the user's timezone, use js and pass it to the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question