When I enter a start and end date for an event, it shows up a day early for both fields. How can I remedy this and show on the correct day?

Comments

brij2136 created an issue. See original summary.

rfulcher’s picture

I am getting this same behavior.

rfulcher’s picture

I was able to determine that in my site this line of code was causing the issue, it is on line 180.

$start = $timezone_service->utcToLocal($start_date, $timezone, 'Y-m-d');

All the dates that were getting converted were going back a day. Not sure why. I tried to debug it but could not so I just replaced that line with this one for now.

$start = $start_date;

I hope this helps.

utcbrij’s picture

Thank you, rfulcher! That worked!

utcbrij’s picture

Status: Needs work » Fixed
utcbrij’s picture

Status: Fixed » Needs work

The start date is good, but the end date is still a day early. Tried editing the end date code in similar fashion as the start date on lines 237 and 257 to no avail. Any ideas?

utcbrij’s picture

Not sure if this is right, but it worked. Please advise if something else should be done.

Line 237 changed from:

$end = $timezone_service->utcToLocal($end_date, $timezone, 'Y-m-d', '+1 day');

to:

$end = $timezone_service->utcToLocal($end_date, $timezone, 'Y-m-d', '+2 day');

Repeated the same for the range on line 257.

rfulcher’s picture

Thanks for the info on the end date brij2136. I am not using the end date at the moment for my project so I did not even look at that. I assume I will need to at some point. I am not sure why we are getting the result that we are. I am using a custom entity and the definition for my field is below. I don't think there is anything wrong with my entity or storage. I am on the east coast of the United States so Daylight Savings time is a possibility. But right now I have a few deadlines I have to hack it and get it out and maybe can look back at it later.

$fields['startdate'] = BaseFieldDefinition::create('datetime')
      ->setLabel(t('Start Date'))
      ->setDescription(t('The start date of the Time off.'))
      ->setSettings([
       'datetime_type' => 'date',
      ])
      ->setDefaultValue([
        'default_date_type' => 'now',
        'default_date' => 'now',
      ])
      ->setDisplayOptions('view', [
       'label' => 'above',
       'type' => 'string',
       'weight' => -4,
      ])
      ->setDisplayOptions('form', [
       'type' => 'datetime_default',
       'weight' => -4,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setRequired(TRUE);
utcbrij’s picture

East coast USA here too. Didn't think of daylight savings being a factor. Thanks for the help. I'm using a custom entity as well, but am new to D8, so I'm still in a learning curve.

eahonet’s picture

I'm experiencing this same issue. I'm using a content type with a Start Date and End Date and it's all skewed by one day. I setup mine using this tutorial: https://www.ostraining.com/blog/drupal/how-to-integrate-a-calendar-in-dr...

peter caritas’s picture

I'm also experiencing this problem. I believe it is a timezone issue. I experienced this issue while I was using a Date type field for both the start and end dates and my timezone was set to CDT (US Central time). I changed my timezone to Melbourne AUS and that corrected the display. I assume that's because it's closer to GMT.

The next thing I tried was to use a different field type for the start and end dates. I switched to Timestamp fields for both dates. Now the events were displaying on the correct day when my timezone was set to CDT. However, if I switched to Melbourne, AUS, then the events appear a day later!

So, it appears that there needs to be some improvement to the timezone handling and it needs to take into account the field type. I would be interested to hear from the maintainer about what approach was used. I note the that timezone_service method seems to be have been abandoned in favor of this, perhaps:

gmdate("Y-m-d\TH:i:s", strtotime($start_date))

tomhung’s picture

My site was doing the same thing. I couldn't find the location of #3 so I overwrote the return of utcToLocal to just return the date submitted. It seem to me that my view is already sending the timezone corrected dates.

  /**
   * Return the value of the converted date from UTC date.
   * fullcalendar_view/src/TimezoneService.php
   */
  public function utcToLocal($utc_date, $local_timezone, $format = DATE_ATOM, $offset = '') {
    // UTC timezone.
    $utc = new \DateTimeZone("UTC");
    // Local time zone.
    $localTZ = new \DateTimeZone($local_timezone);
    // Date object in UTC timezone.
    $date = new \DateTime($utc_date, $utc);
    $date->setTimezone($localTZ);

    if (!empty($offset)) {
      $date->modify($offset);
    }

    // old return
    //return $date->format($format);
    // bypass UTC conversion
    return $utc_date;
  }
utcbrij’s picture

tomhung, great solution at #12. My date problem came back either after the latest DST change or the latest update; not sure which, but your solution worked for me. Thanks.

utcbrij’s picture

Status: Needs work » Fixed
mingsong’s picture

Thanks guys for raising this issue and all the investigations you have done.

Drupal 8 stores all date time field in UTC timezone regardless the timezone of current user. But apparently, in this use case, we don't need to convert the start time from UTC to user's local timezone for some reasons.

I am not able to reproduce this issue on my site. If anyone who is facing this issue could provide more details, that would help me get better understanding what causes this issue. It would be great if you could tell me what value of the start field stored in our database. For example, the value stored in your database is "'2019-02-24T23:00:00'" and the start day appeared in your calendar is "2019-02-25 10:00:00am". That will tell us if the day time stored in your database is the same as it appears in the calendar.

What type of field is the start field and end field? Are they date only? Did you specify the time zone for the user or the default timezone for the entire site? Did you specify the display timezone for the field in the view setting?

The approaches from #3 and #12 are only suitable to the particular use case in which the date stored in the database is not UTC time.

mingsong’s picture

Status: Fixed » Needs work

  • Mingsong committed f8e4e8c on 8.x-2.x
    Issue #2999464: Days showing on the day before start date and end date
    
mingsong’s picture

Ok, I got it.

Drupal changed the behaviour that it won't store date only field in UTC time after upgrading to 8.6. It literally store the original date string into the database regardless the time zone.

So I think I fix this issue with the latest Dev.

mingsong’s picture

Status: Needs work » Fixed
mingsong’s picture

Released with 8.x-2.1

utcbrij’s picture

Perfect, Mingsong! Works like a charm! Thank you!

utcbrij’s picture

Spoke too soon. Trouble on the ipad. Only showing "Select Language" dropdown?

mingsong’s picture

Sorry, do you mean that a calendar view render correctly on computer browser, but in the ipad, only the language dropdown show up?
Did you login the website on your iPad? I am wondering if it is a user role issue rather than a cross browser or responsive view issue.

If you could give me more details, that would help the investigation.

utcbrij’s picture

No, I'm not logged in on the ipad. It worked on my Android, though. I had to go back to #12 for the time being.

utcbrij’s picture

And it worked on the desktop, iMac and PC

mingsong’s picture

I got it. It is a cross browser issue with JavaScript ES2015 (ES6).

There is a similar issue raised here #3043799: Not supporting IE 11 version.

I fixed it with version 8.x-2.2.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

carroll_webprog’s picture

StatusFileSize
new110.19 KB

Hello - I fixed this issue by changing some settings.

In my case, I have my site settings set to a default time of 'Denver' - (US Mountain Time). My dates are stored in 'Denver' - (US Mountain Time) in the database, but on the FullCalendar View they were being displayed as UTC - which for me, is 6 hours earlier.

Originally, on the FullCalendar view I created, I had selected 'Denver' as my timezone under Fields->My Event Date Field.

In the Full Calendar View, I changed the timezone setting to UTC instead of Denver under Fields->My Event Date Field.

The FullCalendar View displays the time correctly on the view now. This is how I resolved this issue. I hope this helps someone else. Thanks.