Symfony, Doctrine – Manage user timezone

By default when you use entity field DateTimeType the date is saved in default timezone configured in your php configuration. Sometimes we need to show and manage dates from database in different timezones.

Using code placed below, date in the database will be saved in UTC +00:00 and during every insert/update data, dates will be converted into this timezone. Every time when you will load data from database, dates will be converted into user defined timezone.

How to do this:

  1. Create new field in user entity to save timezone
  2.  Generate setter and getter using command line
  3.  Update database schema using command line
  4. Create new entity field type
  5. Create new event listener
  6. Register new event listener
  7. (optional) Place info about timezone somewhere in your twig template
  8. Define default time zone
  9. Remember to add possibility to edit timezone in registration and user edit
  10. Everything is ready 🙂

If you have any problems or if you have better solution for this –
please place comment below.
Thanks! 😉

I ask you to review this article

Rating 4.7 Stars from 18 reviews

3 thoughts on “Symfony, Doctrine – Manage user timezone”

  1. when i fetch data from database filter by today its fetch wrong. So how can i solve this issue?

Leave a Reply

Your email address will not be published. Required fields are marked *

4 × five =