3

The reason I'm writing this, is because datetime inputs like

<?= $this->Form->input('start', ['class' => 'form-control', 'label' => false, 'interval' => 5]); ?>

should always display current user time, but they don't.

What I do, is that I associate user object with a timezone: i.e. Europe/Warsaw. Every user in my application has this timezone saved in the database.

In order for the application to keep all the dates and times correct, I've set default timezone in bootstrap.php and also for the app.php/ default datasource to 'UTC'.

When I read an object from a database, I get a UTC timestamp datetime. I keep all the datetimes in DATETIME fields.

For example, I read a task record with 'start' field value: '2017-10-10 12:41:17', and 'end' field value: '2017-10-10 13:41:17' (an hour-long task). That's simple.

But If that task is displayed to a user with a different timezone, let's say it's 'Europe/Warsaw', I need to get those datetimes with proper user timezone.

If I go with:

<?= $this->Form->input('start', ['class' => 'form-control', 'label' => false, 'interval' => 5, 'value' => (new Cake\i18n\Time($task->start))->setTimezone($this->request->session()->read('Auth.User.timezone'))]); ?>

datetimes are correct for a user, but the PROBLEM begins, when I want to save the task, even though the 'start' has not been modified.

What happens?

The 'start' value gets updated with +2 hours according to the user's timezone offset.

Questions are:

  1. Do I properly rely on setting each user timezone like I do with the field 'timezone' within user table row?
  2. How do I keep all the datetimes in the database in UTC, and serve them for display as current logged in user timezone?
  3. How can I display a datetime in CakePHP form input for the user's timezone, and save it to database with the 'UTC' timezone?
  4. How can I set a timezone/locale for the current user to safely display datetimes across the entire application?

Tried: https://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#parsing-localized-datetime-data Didn't work as datetimes didn't update, they kept increasing by the user timezone.

JJLemonova
  • 77
  • 5
  • After submit 'what you get start value'? First check it ?? – A.A Noman Oct 11 '17 at 12:40
  • After I submit the form, I get what I see in the form. If I set the timezone I get "timezoned" array of datetime select input, if I don't set the timezone, I get non-timezoned values. – JJLemonova Oct 11 '17 at 12:45
  • I have faced same problem when I set default timezone. I get datetime what is 6 hours left. So you should select your own timezone. – A.A Noman Oct 11 '17 at 13:07
  • I cannot set the default timezone as any other than UTC. I need to stick to UTC as a base timezone for database. I just want other users to see those UTCs as their timezoned datetimes. – JJLemonova Oct 11 '17 at 13:08
  • see this post https://stackoverflow.com/questions/409286/should-i-use-field-datetime-or-timestamp. May be it is helpful for you – A.A Noman Oct 11 '17 at 13:11
  • Thanks, but the only thing I need is to figure out how to display form input value with a timezone, a do the rest as usual with the basic timezone set in bootstrap.php – JJLemonova Oct 11 '17 at 13:50
  • Currently there is no automatic conversion, that's an long standing [**open issue**](https://github.com/cakephp/cakephp/issues/6571). For now you have to take care for that on your own, for example using a custom database type ([**here's an example**](https://gist.github.com/ndm2/13ea098bef56f0ddd63ed68514eceb3e)), by modifying your UI so that it sends the dates back in UTC, etc. I'm busy right now, but I can write an answer with a working example later on. – ndm Oct 11 '17 at 13:59

0 Answers0