Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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:
- Install a clean Drupal 7 base
- Install Views
- Enable "archive" default view
- Create an article node
- 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.
Comment | File | Size | Author |
---|---|---|---|
#21 | 894560-21-views-date_format-notice.patch | 2.22 KB | Alan D. |
#17 | 894560-17-views-date_format.patch | 748 bytes | mstrelan |
#13 | views_894560-13.patch | 3.4 KB | dale42 |
#9 | archive_view_894560-9.patch | 3.37 KB | dale42 |
#3 | views.patch | 2.53 KB | Chris Gillis |
Comments
Comment #1
Chris Gillis CreditAttribution: Chris Gillis commentedThe 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
Comment #2
dawehnerPlease provide a real patch. So it's easier for other people to test the patch.
Comment #3
Chris Gillis CreditAttribution: Chris Gillis commentedOkay, 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?
Comment #4
bojanz CreditAttribution: bojanz commentedDoesn't apply.
The patch needs to be made from the root directory, so that I can do this:
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.
Comment #5
dawehnerDisclaimer: i'm not online :(
So what does the d6 code of format_date
So here it adds nothing to the timestamp in the current code.
What does d7:
There is a difference between adding 0 and using the user's time zone from my perspective
Comment #6
Chris Gillis CreditAttribution: Chris Gillis commented@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.Comment #7
dawehner@Chris Gillis
I understand that this does not report an error. But the question is does the code the same as in d6?
Comment #8
bojanz CreditAttribution: bojanz commentedIn 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.
Comment #9
dale42I 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.
Comment #10
dale42Comment #11
dawehnerShouldn't this be the php server time zone? This might change from UTC
Comment #12
dale42The 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:
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:
Then in the method:
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.
Comment #13
dale42To 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().
Comment #14
dawehnerThanks. Commited.
Comment #16
nathanjo CreditAttribution: nathanjo commentedI still encounter this error. Views 7.x-3.3.
Contextual Filter: Created year + month
When you are returning 'all' it will result an error
Comment #17
mstrelan CreditAttribution: mstrelan commentedI'm experiencing the same as #16. Patch attached.
Comment #18
Jason Dean CreditAttribution: Jason Dean commentedI applied #17 but still get the error when using 'all' argument (the view still works fine, displaying all results).
Comment #19
dawehnerThe 15 definitive needs some explanation.
Additional the patch seems to produce some problems, so needs work.
Comment #20
mstrelan CreditAttribution: mstrelan commentedWow, I have no idea why I have that . "15" in there :(
That is bizarre.
Comment #21
Alan D. CreditAttribution: Alan D. commentedThe 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.
Comment #22
Alan D. CreditAttribution: Alan D. commentedAh. Today's error was using "Created year + month", the first patch was for the more generic "Created date - CCYYMMDD"
Comment #23
darvanenOne 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.
Comment #24
Alan D. CreditAttribution: Alan D. commentedDoes this need to be bumped to D8 core before backporting; is it even an issue there?
Comment #25
joelstein CreditAttribution: joelstein as a volunteer commentedI also encountered this issue when the date argument was some invalid value (like "0.85"). This patched fixed the issue for me. Thanks!
Comment #26
riddhi.addweb CreditAttribution: riddhi.addweb at AddWeb Solution Pvt. Ltd. commentedComment #27
DamienMcKennaConfirmed via the strtotime() documentation that this is the correct way of handling it.
Comment #29
DamienMcKennaCommitted.
Comment #30
Alan D. CreditAttribution: Alan D. commentedThis 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)
Comment #31
DamienMcKennaYeah, I don't see an issue that contains "strtotime" and is related to this problem.
Comment #32
DamienMcKennaFYI similar code is in: