Problem/Motivation

Given a date field, with the 'Date only' setting and 'Current date' as the default, the current date can be wrong for the configured time zone. It works as expected if the field is set to 'Date and time'

Screenshot of two date fields, one with Date only and one with Date and time, using Current date as the default, with timezone set to Los Angeles:

Same node, with the timezone set to UTC:

Steps to reproduce

Not sure whether it matters what time this is tested, but my test was done at 7:33pm PST.

  1. Install Drupal
  2. Add a Date field to the article content type, with 'Date only', and the default set to 'Current date'
  3. Add another date field with 'Date and time' and the default set to 'Current date'
  4. Set the site timezone to Los Angeles
  5. Create a node, see that the date is different between the fields
  6. Set the timezone to UTC
  7. See that the date is correct

Proposed resolution

Figure out why the default date is wrong and fix.

Remaining tasks

  1. Find the cause of this issue
  2. Write a patch with tests
  3. Review

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Original report by charlie1volley

Hi,

New to Drupal. I searched and read about various date/time issues but I'm sure if this is a similar issue to any of them or if/what to do about it?

What are the steps required to reproduce the bug?

Create date field in content type and default to 'current date'. Select settings 'date only'. When I create content (for example right now its 7/23/2019 9PM PST) it shows 7/24/2019. If I change the field setting to 'date and time' though it returns the correct date and time.

What behavior were you expecting? The date to be today in PST.

What happened instead? Incorrect date, a day ahead.

Thanks for any help you can provide on this...

Comments

charlie1volley created an issue. See original summary.

charlie1volley’s picture

Sorry, should have added that config time zone is set to 'Los Angeles' and I tried checking and unchecking the 'users may set their own time zone' box.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

pameeela’s picture

Title: Date not rendering correctly in created content » 'Current date' default for date only field can be off depending on time zone setting
Issue summary: View changes
Issue tags: +Bug Smash Initiative
StatusFileSize
new7.9 KB
new8.05 KB

Updated IS with steps to reproduce. I suspect this may have something to do with the 'Date only' getting a time set of 12:00?

pameeela’s picture

Issue summary: View changes
adityasingh’s picture

This I believe is as per expected behaviour as can be seen in the following documents in core/modules/datetime/src/DateTimeComputed.php file.

// If the format did not include an explicit time portion, then the
// time will be set from the current time instead. For consistency, we
// set the time to 12:00:00 UTC for date-only fields. This is used so
// that the local date portion is the same, across nearly all time
// zones.
// @see \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime()
// @see http://php.net/manual/datetime.createfromformat.php

and this link
https://www.php.net/manual/en/datetime.createfromformat.php#110385

atul4drupal’s picture

Status: Active » Closed (works as designed)

Thanks @pameeela & @adityasingh you have it rightly pointed, the issue is because by default time is set to 12:00:00

File: core/lib/Drupal/Component/Datetime/DateTimePlus.php
@line: 711

 /**
   * Sets the default time for an object built from date-only data.
   *
   * The default time for a date without time can be anything, so long as it is
   * consistently applied. If we use noon, dates in most timezones will have the
   * same value for in both the local timezone and UTC.
   */
  public function setDefaultDateTime() {
    $this->dateTimeObject->setTime(12, 0, 0);
  }

This is actually causing the issue to occur for few hours as long the date differs between local time zone (if other that UTC) and UTC.

One possible solution might be to adjust the default fallback time according to sites local time zone i.e get +/- in time from UTC time for current timezone and accordingly set the default time for the date field.

Feel free to share suggestions.

atul4drupal’s picture

Status: Closed (works as designed) » Active

Mistakenly had it closed...

scott.whittaker’s picture

Just ran into this one myself. It seems the most logical solution to me would be to set the datetime created to midnight in the current timezone. What is the point of standardising the time portion when it makes the date portion incorrect, and that's the only portion we actually care about.

It's hard to see how this buggy behaviour could be considered "works as designed".

wombatbuddy’s picture

StatusFileSize
new974 bytes

In case of the 'Date only' setting the 'UTC' is used instead of user's timezone. It looks like this is the reason of the issue.

wombatbuddy’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: 3069854-11.patch, failed testing. View results

pameeela’s picture

Status: Needs work » Closed (duplicate)

Duplicate of #2993165: Date Only field shows incorrect default value when UTC date is different than user's date and that was created earlier and has more activity, so closing this one.

wombatbuddy’s picture

@pameeela, Is it make sense to transfer the patch from this issue there? It seems is different from those offered in that issue.

pameeela’s picture

@wombatbuddy, yes that's a good idea, a new patch might just revive the issue :) It is failing though, not sure whether the fails are random or not, but if not would be good to address them before you transfer it. Thanks!