David Garcia
2 min readAug 24, 2023

--

Hello, @dotcom.software

First things first, thanks for stopping by and trying to share your knowledge. Having said that...

I believe you are wrong to assume "the only correct way of storing a date is to keep it in UTC". Why? Because...

1. If that were the case, the world would work only with UTC instead of having many different time zones.

2. Single-country products don't need UTC date/time data but localised date/time data. So, instead of UTC, they might need Europe/Berlin (just an example).

3. If someone works from 9 am to 5 pm in the UK, they will never have the exact working times as someone from 9 am to 5 pm in Romania (because the UK timezone is BT/BST and Romania timezone is EET/EEST).

4. If unset, Doctrine takes time zones from PHP. If unset, PHP takes time zones from the server. And yet, you can pass a DateTimeZone object when instantiating a DateTime object. And Doctrine doesn't change it to UTC "because it is the silver bullet".

5. Even assuming they should always be stored in UTC format, the biggest mistake in your comment is that you are judging a workaround or specific scenario without knowing the company and project needs.

Even if you come back again with a comment like "you can calculate it with a getter instead of storing it" based on the given User entity as an input argument, that might not work because:

a) The User entity might not be loaded at that point if using custom user providers through in-memory or text-based repositories instead of running a Doctrine DQL query and

b) The DQL queries - like $query->getResult() - require post-query processing to map all the data into objects, especially with nested items (please run Blackfire for results), and you can save a lot of time by simply returning an array of plain data - $query->getArrayResult() - with a pre-calculated value.

The next time you try to teach someone, please ensure you get enough insights and consider the big picture. All the small details matter, and you should remember them when building your code.

Thanks for sharing your thoughts and expertise, and I also hope you have learned new things and edge cases to consider from this discussion.

--

--

David Garcia
David Garcia

Written by David Garcia

Senior Software Engineer, Backend, NodeJS & Symfony developer, workaholic, passionate for new technologies and OSS contributor. https://linktr.ee/davidgarciacat

Responses (3)