too long

Symfony 4 has a form type called DateTimeType (not DBAL) that should automatically convert timezones, but for some annoying reason this doesn't seem to work for me.

Here are things I tried / know about

// I tried setting this to my timezone: 'Europe/Brussels'
date_default_timezone_set('UTC'); 
// Setting the timezone doesn't change the timestamp (only 'by reference' for displaying purposes. so the stored datetime is still UTC!
$date->setTimezone(new Timezone('Europe/Brussels'))

And for my form I tried:

$builder
    // other fields omitted
    ->add('start', DateTimeType::class, array(
        'label'             => 'Start',
        'widget'            => 'single_text',
        'html5'             => true,
        'input'             => 'datetime',
        'model_timezone'    => 'UTC',
        // Timezone stored in $user->getTimezone() and set to 'Europe/Brussels'
        // The timezone is passed correctly while debugging
        'view_timezone'     => $options['timezone'],
        'invalid_message'   => 'Invalid date'
    ))

The problem with this setup is that every date fetched from the database is passed to the form in UTC. For example:

  • The DateTime's value is "2018-06-01T12:00:00+00:00"

Because the user's timezone is set to Europe/Brussels

  • The time should be: "2018-06-01T12:00:00+02:00"
  • or "2018-06-01T14:00:00+00:00"

Since this conversion is not happening, I tried to implement the desired behaviour using moment, moment-timezone and jQuery:

// _timezone is a global variable set to the user's timezone ('Europe/Brussels')
const dtStart = moment(event.start).tz(_timezone);
dteEventStart.val(dtStart.format(moment.HTML5_FMT.DATETIME_LOCAL));

This results in "2018-06-01T14:30" which is the time we want to display to the user according to his timezone. But when the user submits the form, symfony doesn't convert the date to UTC, resulting in a stored date of "2018-06-01T14:00:00+00:00". As you can guess, the next time the user queries the database, the date is converted to "2018-06-01T16:00:00+00:00" thanks to moment.

To counter this, I wanted to change the timezone back to UTC using moment:

$('form[name="event"]').submit(function (event) {
    const form = $(this);
    // Loop through all date related input types
    form.find('input[type="date"], input[type="datetime"], input[type="datetime-local"]')
        .each(function (index, element) {
            // Let moment convert all date[times] to UTC
            element.value = moment(element.value).tz('UTC').format(moment.HTML5_FMT.DATETIME_LOCAL);
        });
    return true;
});

I can see the value change to the correct timezone before submission, but after all this, symfony throws an exception telling me that start cannot be empty :(

Any suggestions? Sorry for the long question.