English version was created automatically using Drupal module auto_node_translate and free DeepL translator.
Drupal and Datetime / Timestamp fields - how to correctly store and print dates according to timezone settings in PHP
zveřejněno 2020-07-24
Drupal 8 (and now Drupal 9) stores Datetime fields in the format 2020-07-23T13:58:36, where the automatic conversion to UTC time occurs. There may then be some problems with this.
Introduction
If you are building a site where users can set their own timezone, you may soon start solving problems about how to actually store the time correctly in the database. It's not rocket science, but there are a few things to watch out for. The trick is to set UTC for DrupalDateTime.
Before we get started
To minimize the possibility of errors, it's best to add a datetime field to the user's datetime that we'll set right away. To do this, it's also good to have access to the database, so you can keep track of how the value changes depending on the time zone you choose. And ideally, it's great to have access (for example, via a remote desktop) to a computer that is actually in a different time zone.
To do this, prepare some sort of View that will output the value of this datetime field.
Using the database and View, we can test everything thoroughly until we are sure that everything is working as it should. Now we can try again, this time using our own code.
How to save the datetime field
I have solved the case where the user saves his profile, so the current time should be saved. The resulting code is very simple:
use Drupal\Core\Datetime\DrupalDateTime;
$date_object = new DrupalDateTime('now', 'UTC');
$user->set(field_datum, $date_object->format('Y-m-d\TH:i:s'));
But it took quite a while to test that everything worked correctly.
The best thing to do is to find a field in the database, and look there to see if the time is indeed in UTC format. So currently we have, for example, daylight saving central European time. So compared to UTC, we're two hours ahead. This means that if I store the time at 8:35, it should be 6:35 in the database.

But note, this only applies if the user in question has set the city/timezone in that timezone.

The moment the user has, for example, set London, a different timezone, the saved value will already be 7:35.
Take attention to this, if you, as for example an administrator, with a time zone set, change some datetime field of another user with a different time zone, it will follow that administrator setting.
How to list a datetime field
When listing a field, we need to define again that we are using UTC. Then we can list it in any date format.
use Drupal\Core\Datetime\DrupalDateTime;
$date='2020-07-24T06:35:23';
$date_original= new DrupalDateTime( $date , 'UTC' );
$ts = $date_original->format('U');
$date = \Drupal::service('date.formatter')->format($ts, 'date_and_time');
Conclusion
It's not rocket science after all, but it's not that trivial. The numerous discussion posts on the subject show that.
A few useful links:
Programmatically set a datetime field https://drupal.stackexchange.com/questions/215332/programmatically-set-…
New Datetime API https://www.drupal.org/node/1834108
The Time Zone Converter https://www.thetimezoneconverter.com/