title: Symfony convert to user's timezone in controller or twig
body: <p>I store in my database dates most of the time with DateTime on MySQL on UTC time. I'm using Symfony 2.4.5 (yes it is old).</p>
  
  <p>I retrieve dates from the controller with normal sql query from Propel.
  Since I do not want to query the user timezone all the time from the database I will save in the session the user's timezone '{'timezone'=> 'Europe/Paris'}.</p>
  
  <p>Should I convert the dates retrieved from DB in the controller or do that in Twig ?</p>
  
  <pre><code>// In controller
  $dictionaryThatHaveOtherData = ...
  $dateFromDbInUTC = UserQuery::Create()-&gt;...
  $dateFromDbInUTC-&gt;setTimeZone(new DateTimeZone($this-&gt;get('session')-&gt;get('timezone')));
  $dictionaryThatHaveOtherData['dateForSomething'] = $dateFromDbInUTC;
  // In Twig
  {{ dateForSomething | date('theFormatIWant') }}
  </code></pre>
  
  <p>Or instead do the convert in Twig:</p>
  
  <pre><code>// In controller
  $dictionaryThatHaveOtherData = ...
  $dateFromDbInUTC = UserQuery::Create()-&gt;...    
  $dictionaryThatHaveOtherData['dateForSomething'] = $dateFromDbInUTC;
  // In Twig
  {{ dateForSomething | date('theFormatIWant', app.session.timezone) }}
  </code></pre>
  
  <p>I guess it is better to do in controller as the view shall only care about showing data in desired format and not involves to many things like thinking about if the date is already in good timezone or not.</p>
  
  <p>What about if the view needs to do comparisons between dates, would it be better in UTC or user timezone ? Does it make a difference ?</p>
  
  <hr>
  
  <p>EDIT since answer from DarkBee:</p>
  
  <p>First I tried in twig view:</p>
  
  <ul>
  <li>date('H:i:s', user_timezone.zone)</li>
  <li>date('H:i:s', app.session.get('timezone'))</li>
  <li>date('H:i:s')</li>
  <li>date('H:i:s', 'Europe/Paris')</li>
  <li>date('H:i:s', 'Asia/Shanghai')</li>
  <li><p>date('H:i:s', false)</p>
  
  <p>Send to view with UTC timezone</p></li>
  <li><p>19:00:00</p></li>
  <li>19:00:00</li>
  <li>11:00:00</li>
  <li>13:00:00</li>
  <li>19:00:00</li>
  <li><p>11:00:00</p>
  
  <p>Send to view with Shanghai timezone </p></li>
  <li><p>19:00:00</p></li>
  <li>19:00:00</li>
  <li>11:00:00</li>
  <li>13:00:00</li>
  <li>19:00:00</li>
  <li><p>19:00:00</p>
  
  <p>In controller : <code>$this-&gt;get('Twig')-&gt;getExtension('core')-&gt;setTimezone('Europe/Paris');</code> </p></li>
  <li><p>19:00:00</p></li>
  <li>19:00:00</li>
  <li>13:00:00</li>
  <li>13:00:00</li>
  <li>19:00:00</li>
  <li><p>11:00:00 </p>
  
  <p>In controller: <code>$this-&gt;get('Twig')-&gt;getExtension('core')-&gt;setTimezone($this-&gt;get('session')-&gt;get('timezone'));</code></p></li>
  <li><p>19:00:00</p></li>
  <li>19:00:00</li>
  <li>19:00:00</li>
  <li>13:00:00</li>
  <li>19:00:00</li>
  <li><p>11:00:00</p>
  
  <p>In controller and date with shanghai timezone : <code>$this-&gt;get('Twig')-&gt;getExtension('core')-&gt;setTimezone('Europe/Paris');</code> </p></li>
  <li><p>19:00:00</p></li>
  <li>19:00:00</li>
  <li>13:00:00</li>
  <li>13:00:00</li>
  <li>19:00:00</li>
  <li>19:00:00</li>
  </ul>
  
  <p>So yes we see that 'date('H:i:s')' is the good time when I set twig timezone</p>
  
  <p>Thank you, I guess I managed to make it work but I'm not sure if this is good and will not have problems with multi-users (I do not know how symfony works with multi-thread or multi-request from different users) at the same time as I do:</p>
  
  <pre><code>$this-&gt;twig-&gt;getExtension('core')-&gt;setTimeZone($user-&gt;getTimezone()-&gt;getZone());
  </code></pre>
  
  <p>I tried with two browsers fetching the different pages and setting a new timezone for each different user logged in. It works and does not override the other user's timezone. I thought it would because it changes the default timezone for twig core but I removed the line I modify the timezone, after the user logged in and has set already the value, I reloaded the page and it was set back to UTC.</p>
  
  <p><strong>Does it mean that those global variables are only for one request ?</strong></p>
  
  <p>I am not used to symfony2 for handling requests and how it works for variables and cache.</p>
  
  <p>I looked for many solutions:
  <a href="https://stackoverflow.com/questions/9990625/symfony2-inject-current-user-in-service">example</a>
  <a href="http://io.ofbeaton.com/2015/06/symfony-2-6-user-timezones/" rel="nofollow noreferrer">example2</a>
  <a href="https://stackoverflow.com/questions/9886058/how-can-i-set-the-default-date-format-for-twig-templates-in-symfony2">example3</a></p>
  
  <p>My code:</p>
  
  <pre><code>class TwigDateRequestListener
  {
      protected $twig;
      private $context;
  
      function __construct(\Twig_Environment $twig, SecurityContext $context)
      {
          $this-&gt;twig = $twig;
          $this-&gt;context = $context;
      }
  
      public function onKernelRequest(GetResponseEvent $event)
      {
          $user = $this-&gt;context-&gt;getToken()-&gt;getUser();
          if ($user != 'anon.') {
              $user = SaasUserQuery::create()-&gt;findPk($user-&gt;getId());
              $this-&gt;twig-&gt;getExtension('core')-&gt;setTimeZone($user-&gt;getTimezone()-&gt;getZone());
          } else {
              $this-&gt;twig-&gt;getExtension('core')-&gt;setTimeZone('Europe/Paris');
          }
      }
  }
  </code></pre>
  
  <p>Then in the view I only need to write 'date('H:i:s')', and it is the user's timezone and if not logged in on Paris timezone.</p>
  
  <p><strong>Is it good? I am not sure about multi-request from different users at the same time.</strong>
  For example:</p>
  
  <p><strong>Important:</strong>
  One user make a request he is in the controller action method (the timezone has been set), a second user make a request he has a different timezone than the first user. Now both controller make the call to render and the view is rendered and returned to users.</p>
  
  <p>Precisely here, what happens ? Both get good dates in their respective timezone or the first user get dates in the second user timezone ?</p>