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:
- Do I properly rely on setting each user timezone like I do with the field 'timezone' within user table row?
- How do I keep all the datetimes in the database in UTC, and serve them for display as current logged in user timezone?
- How can I display a datetime in CakePHP form input for the user's timezone, and save it to database with the 'UTC' timezone?
- 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.