The following warnings messages appear on the Drupal 7 Views default archive view:

  • * Warning: timezone_open(): Unknown or bad timezone (0) in format_date() (line 1855 of /Users/dale/Sites/d7/includes/common.inc).
  • * Warning: date_timezone_set() expects parameter 2 to be DateTimeZone, boolean given in format_date() (line 1882 of /Users/dale/Sites/d7/includes/common.inc).

These errors can be reproduced as follows:

  1. Install a clean Drupal 7 base
  2. Install Views
  3. Enable "archive" default view
  4. Create an article node
  5. Go to the /archive page

If you create a second node and backdate it to a previous month, the date_timezone_set() warning occurs twice. It appears to be tied to the number of different months being displayed.

The warnings also appear if you click through from the summary page to the month view page. For example, the warnings on both /archive and /archive/201008.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Chris Gillis’s picture

Status: Active » Needs review

The error lives in views_handler_argument_dates_various.inc

Everywhere format_date() is called, we are passing it 0 when it expects NULL (the function does an isset() check). I can resolve the issue by modifying the code on lines 22, 29, 66, 73, 104, 133 and 141.

For instance, instead of
return format_date(strtotime($created), 'custom', $this->format, 0);
We need
return format_date(strtotime($created), 'custom', $this->format, NULL);

Sorry I'm not a cool CVS patcher dude. :P

dawehner’s picture

Status: Needs review » Needs work

Please provide a real patch. So it's easier for other people to test the patch.

Chris Gillis’s picture

Status: Needs work » Needs review
FileSize
2.53 KB

Okay, so I've just given myself a quick crash course in CVS and here is my first patch. Can someone tell me if I've done it correctly?

bojanz’s picture

Doesn't apply.

The patch needs to be made from the root directory, so that I can do this:

cd views/
patch -p0 < views.patch

I suggest using cvs or git to roll the patch (make the changes in a CVS checkout, then "cvs diff -up > views.patch", for example).

Other than that, the code changes are RTBC. Did a grep, those are the only places still left with the D6 syntax.

dawehner’s picture

Status: Needs review » Needs work

Disclaimer: i'm not online :(

So what does the d6 code of format_date

  if (!isset($timezone)) {
    global $user;
    if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
      $timezone = $user->timezone;
    }
    else {
      $timezone = variable_get('date_default_timezone', 0);
    }
  }

  $timestamp += $timezone;

So here it adds nothing to the timestamp in the current code.

What does d7:

$timezone Time zone identifier; if omitted, the user's time zone is used.

There is a difference between adding 0 and using the user's time zone from my perspective

Chris Gillis’s picture

@bojanz
Sorry, I have no idea how to do that. I am using TortoiseCVS on Windows.

@dereine
The original error was caused by using 0 as a timezone (hence Unknown or bad timezone (0)). By setting it to NULL (the default value) it will run through and get the user's timezone.

dawehner’s picture

@Chris Gillis

I understand that this does not report an error. But the question is does the code the same as in d6?

bojanz’s picture

In any case, the patch is consistent with how format_date is used elsewhere in D7 Views (we don't pass the timezone anywhere). It's debatable whether this is correct, but it's at least consistent.

dale42’s picture

FileSize
3.37 KB

I agree with dereine, using NULL is not equivalent to using 0.

In Drupal 6 format_date: $timezone Time zone offset in seconds; if omitted, the user's time zone is used.

In Drupal 7 format_date: $timezone Time zone identifier; if omitted, the user's time zone is used.

The Drupal 7 equivilent of 0 is UTC.

There's a number of functions that format date components (month/date/year) using a date that is fixed except for the component being formatted. For example:

return format_date(strtotime("2005" . $month . "15" . " 00:00:00 UTC"), 'custom', $this->format, 0);

In these instances it's clear the timezone should be 'UTC' to match the input date.

Making this change resolves the issue for the default archive view (on my system).

I'm not sure how to handle the summary_name() and title() functions in views_handler_argument_node_created_fulldate. It's less clear from the code what the intended timezone should be. Since UTC is the equivilent of 0, I've made the change to UTC. I'm not sure this is the best way to go.

While testing I discovered a possible problem wth views_handler_argument_node_created_fulldate::summary_name() getting bad data. Out of time for today, but will check further tomorrow and create a new issue if required.

I've attached a patch that makes the 0 -> UTC change.

dale42’s picture

Status: Needs work » Needs review
dawehner’s picture

Shouldn't this be the php server time zone? This might change from UTC

dale42’s picture

The Drupal server timezone was my other choice for the class views_handler_argument_node_created_fulldate. I went with UTC because "UTC" in Drupal 7 format_date() is the equivalent of 0 in Drupal 6 format_date(). But maybe the code did that just so format_date() wouldn't use the user's timezone setting? We could also go:

   $timezone = variable_get('date_default_timezone', date_default_timezone_get())
   return format_date(strtotime($this->argument), 'custom', $this->format, $timezone);

However, the more I think about it, I think a better way to go is changing:

return format_date(strtotime($created), 'custom', $this->format, 0);

to:

return format_date(strtotime($created . " 00:00:00 UTC"), 'custom', $this->format, 'UTC');

For the classes views_handler_argument_node_created_year_month, views_handler_argument_node_created_month, and views_handler_argument_node_created_day, it should be UTC.

Take, for example, the class views_handler_argument_node_created_day. In the constructor:

        $this->format = 'j';
    $this->arg_format = 'd';

Then in the method:

  function summary_name($data) {
    $day = str_pad($data->{$this->name_alias}, 2, '0', STR_PAD_LEFT);
    // strtotime respects server timezone, so we need to set the time fixed as utc time
    return format_date(strtotime("2005" . "05" . $day . " 00:00:00 UTC"), 'custom', $this->format, 'UTC');
  }

The comment // strtotime respects server timezone, so we need to set the time fixed as utc time is from the original code, and not a comment I added.

Since the input date for strtotime() is UTC, the date should be formatted in the UTC timezone. If not, any UTC "minus" timezone like America/Vancouver (UTC - 8hrs) would create an off-by-one error.

I think views_handler_argument_node_created_day and views_handler_argument_node_created_fulldate are the only classes it matters for. The other classes use 15 as the day, so no timezone would create an off-by-one error for the month or year.

dale42’s picture

FileSize
3.4 KB
  • The code producing the warning is using strtotime()/format_date() to convert the view date argument to a date string
  • If the timezone isn't specified in the input of the strtotime() function, it uses the timezone set with date_default_timezone_set
  • format_date() will use either the user timezone or the server timezone, depending on settings

To insure both the strtotime() and format_date() functions use the same timezone, the timezone must be explicitly set in each function. This patch does that.

Note, issue http://drupal.org/node/914102 effects the testing of the code in views_handler_argument_node_created_fulldate().

dawehner’s picture

Status: Needs review » Fixed

Thanks. Commited.

Status: Fixed » Closed (fixed)

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

nathanjo’s picture

Version: 7.x-3.x-dev » 7.x-3.3
Status: Closed (fixed) » Active

I still encounter this error. Views 7.x-3.3.

Contextual Filter: Created year + month

When you are returning 'all' it will result an error

Warning: date_timezone_set() expects parameter 1 to be DateTime, boolean given in format_date() (line 1909 of /root/includes/common.inc).

Warning: date_format() expects parameter 1 to be DateTime, boolean given in format_date() (line 1919 of /root/includes/common.inc).
mstrelan’s picture

Version: 7.x-3.3 » 7.x-3.x-dev
Component: Miscellaneous » Code
Status: Active » Needs review
FileSize
748 bytes

I'm experiencing the same as #16. Patch attached.

Jason Dean’s picture

I applied #17 but still get the error when using 'all' argument (the view still works fine, displaying all results).

Warning: date_timezone_set() expects parameter 1 to be DateTime, boolean given in format_date() (line 1937 of mysite/drupal7/includes/common.inc).
Warning: date_format() expects parameter 1 to be DateTime, boolean given in format_date() (line 1947 of mysite/drupal7/includes/common.inc).
dawehner’s picture

Status: Needs review » Needs work
+++ b/modules/node/views_handler_argument_dates_various.incundefined
@@ -33,7 +33,10 @@ class views_handler_argument_node_created_fulldate extends views_handler_argumen
+    $timestamp = strtotime($this->argument . "15" . " 00:00:00 UTC");

The 15 definitive needs some explanation.

Additional the patch seems to produce some problems, so needs work.

mstrelan’s picture

Wow, I have no idea why I have that . "15" in there :(
That is bizarre.

Alan D.’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
2.22 KB

The 15 came from the views_handler_argument_node_created_year_month title() function that does not have a day component :)

I managed to have this triggered in another handler today too, so this includes preemptive strike of the notice on all title() calls in the file.

Alan D.’s picture

Ah. Today's error was using "Created year + month", the first patch was for the more generic "Created date - CCYYMMDD"

darvanen’s picture

One way to deal with this (in d7) is to set your exception value to a recognisable format.

For instance for a "created year + month" contextual filter I asked it to look for '000000' instead of 'all', worked a treat.

Alan D.’s picture

Does this need to be bumped to D8 core before backporting; is it even an issue there?

joelstein’s picture

Status: Needs review » Reviewed & tested by the community

I also encountered this issue when the date argument was some invalid value (like "0.85"). This patched fixed the issue for me. Thanks!

riddhi.addweb’s picture

Issue tags: +date
DamienMcKenna’s picture

Confirmed via the strtotime() documentation that this is the correct way of handling it.

DamienMcKenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed.

Alan D.’s picture

This probably affects D8 but I haven't tried to replicate if the code is exposed.

i.e. same code in Drupal\views\Plugin\views\argument\Date::summaryName() and Date::title()

If so, create a new core issue referencing this thread :)

Edit: Date was the parent class to the 4 classes, which I assumed contained the abstract signatures for those 4 sub-classes, but it doesn't (as noted below)

DamienMcKenna’s picture

Yeah, I don't see an issue that contains "strtotime" and is related to this problem.

DamienMcKenna’s picture

FYI similar code is in:

  • core/modules/views/src/Plugin/views/argument/DayDate.php
  • core/modules/views/src/Plugin/views/argument/FullDate.php
  • core/modules/views/src/Plugin/views/argument/MonthDate.php
  • core/modules/views/src/Plugin/views/argument/YearMonthDate.php

Status: Fixed » Closed (fixed)

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