Datetime element with #date_time_element=none adds current time

Created on 3 February 2023, over 2 years ago

Problem/Motivation

I noticed a strange bug in a custom form using the datetime form element type.

My desire was to collect only a date, without a time. I want to use datetime instead of date because it allows a DrupalDateTime object to be used as the #default_value, which is then also available in validation/submit methods. The docs state that '#date_time_element' => 'none', can be used to "Do not display a time element."

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Datetime%...

However, the resulting DrupalDateTime object in form validation/submit does NOT have a time of "midnight" as I would expect. Instead it is mysteriously populated with the current time (regardless of what the date is set to).

Steps to reproduce

The following code snippet demonstrates a simple form element where this issue occurs:

    $form['datetime'] = [
      '#type' => 'datetime',
      '#title' => 'Datetime',
      '#default_value' => new DrupalDateTime(date('Y-m-d')),
      '#date_time_element' => 'none',
    ];
  • Loading the form shows the date element, with the current date filled in as the default value.
  • The new DrupalDateTime(date('Y-m-d')) creates a date object that does NOT include a time, as is expected. (In other words, it is midnight of the current date.)
  • Upon form submission, I would expect that $form_state->getValue('datetime') would give me a similar date object - with a time of midnight.
  • In reality, the date object that is created ends up with the *current time*, even though that time was no where in the form inputs or default value.

For example, using the above code in an example form right now, it shows an HTML5 date field with a value of "02/03/2023". When I submit the form, however, and run the following code in validate/submit, the result is not what I would expect:

date('c', $form_state->getValue('datetime')->getTimestamp()

Output:

2023-02-03T16:33:06+00:00

If I comment out the '#date_time_element' => 'none', line, however, it works as expected. When the form is built I see the date element with a value of "02/03/2023" and a time element with a value of "12:00:00 AM". And when I submit the form, the result is what I would expect:

date('c', $form_state->getValue('datetime')->getTimestamp()

Output:

2023-02-03T00:00:00+00:00

Proposed resolution

TBD

Remaining tasks

TBD

User interface changes

TBD

API changes

TBD

Data model changes

TBD

Release notes snippet

TBD

πŸ› Bug report
Status

Active

Version

10.1 ✨

Component
FormΒ  β†’

Last updated about 7 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States m.stenta

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @m.stenta
  • πŸ‡ΊπŸ‡ΈUnited States m.stenta
  • πŸ‡ΊπŸ‡ΈUnited States m.stenta

    I traced this to the PHP \DateTime::createFromFormat() method which gets used in the process...

    This is interesting:

    \DateTime::createFromFormat('Y-m-d', '2023-02-03')->format('c');

    Output:

    2023-02-15T17:01:23+00:00

    So the time is actually getting added at a low level (PHP's DateTime object itself).

    I think Drupal should account for this so that form elements with '#date_time_element' => 'none', behave in the way one would expect. That is: they shouldn't let the current date be added if time is omitted.

Production build 0.71.5 2024