Symfony buildForm convert datetime to local and back to utc
<p>So I want to show options to set a date for an activity in local time but save it in UTC as symfony standard. I have 5 different User segments with 5 different Timezones values. A user can set a new activity in a form. Each user entity has it's own timezone set. My question is where do I set the conversion? I have the user entity in a logged in user session state.</p>

<p>In the ControllerAction where I invoke the formBuilder?</p>

<p>In the formBuilder?</p>

<p>The formbuilder is still a little fuzzy for me and I know there's alot happening in it.</p>

<p>The formBuilder, simplified:</p>

<pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
{
    //...
    $builder-&gt;add('name');
    $builder-&gt;add('startDate', DateTimeType::class, array(
        'widget' =&gt; 'single_text',
        'format' =&gt; 'yyyy-MM-dd HH:mm',
        )
    );
    $builder-&gt;add('endDate', DateTimeType::class, array(
        'widget' =&gt; 'single_text',
        'format' =&gt; 'yyyy-MM-dd HH:mm',
        )
    );

    //...

    $builder-&gt;add('submit', SubmitType::class, array('attr' =&gt; array('class' =&gt; 'button')));
}
</code></pre>

<p>The ControllerAction:</p>

<pre><code>public function activityAction(Request $request, Activity $activity){
    $variables = array();
    /**
    * TODO Determine User Local timezone and store as UTC
    *
    */

    $user = $this-&gt;get('security.token_storage')-&gt;getToken()-&gt;getUser();
    $timezone = $user-&gt;getTimezone();

    $activity = new Activity();

    $form = $this-&gt;createForm(ActivityType::class, $activity);
    $form-&gt;handleRequest($request);

    if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {

        $activity-&gt;setName($form-&gt;get('name')-&gt;getData());

        /**
        *
        * Maybe here, (I've just realized)
        * Because earlier, the form just handled the datetime without conversion
        * And just stored them in the DB as they where
        *
        */

        // Example: $timezone = 'Europe/London';
        $date = new DateTime();
        $date-&gt;setTimestamp( $form-&gt;get('startDate')-&gt;getData() );
        $date-&gt;setTimezone(new DateTimeZone($timezone));   
        $activity-&gt;setStartDate($date-&gt;format("Y-m-d H:i:s"));

        $date = new DateTime();
        $date-&gt;setTimestamp( $form-&gt;get('endDate')-&gt;getData() );
        $date-&gt;setTimezone(new DateTimeZone($timezone));   
        $activity-&gt;setEndDate($date-&gt;format("Y-m-d H:i:s"));


        $em = $this-&gt;getDoctrine()-&gt;getManager();
        $em-&gt;persist($activity);
        $em-&gt;flush();
    }
    elseif ($form-&gt;isSubmitted() &amp;&amp; !$form-&gt;isValid() ) {
        $variables['message'] = 'Something went wrong!';

    }

    $variables["title"] = "Create Activty";
    $variables["form"] = $form-&gt;createView();
    return $this-&gt;render('AcmeBundle:SomeViews:activity.html.twig', $variables);
}
</code></pre>

<p>So I believe, I've just answered my own question, which forces me to ask; Is this the convention/best practice how you do it?</p>