Flixible date arguments

KarenS's picture

Since part of the purpose of this group is to track views development, I thought I would share my solution for creating flexible date range arguments for the Date module.

Creating arguments for date fields is hard because there are so many possible ways people might want to use them. You may need to find a full date or a part of a date or you may want to look for a date + duration or maybe specify that the duration will start on the current date or some other date, etc. etc. In creating arguments for dates I have been finding that the list of arguments in views gets longer and longer, but no matter how many you create, someone will always come back and ask for another type of argument.

Webchick at Lullabot contacted me about creating a date range argument for a client she was working with and we came up with an initial plan for an argument that uses an ISO from and to date to specify a date range. As I was working with it I began to see ways to make it even more flexible by stipulating a duration instead of a to date. Then I found out that the ISO 8601 standard also has specifications for indicating date duration and time periods (I didn't realize that before) so I used the ISO standards as specifiers. I needed a way for people to get the current date and the ISO standard doesn't have that, so I set the argument up to use the @ sign as a substitute for the current date and time.

Some examples of the kinds of arguments you can use and the results you will get:.


 * --------- ARGUMENT --------  >> -------- RESULTING QUERY RANGE --------
 * 2006-W24                     >> 24th ISO week in 2006
 * 2006                         >> the whole year of 2006
 * 2006-03                      >> the whole month of Mar 2006
 * 2006-02--2006-03-15          >> Feb 1 2006 to Mar 15 2006
 * 2006-08-31T14:00:00--P2H     >> the 14th to 16th hours of Aug 8 2006
 * @--2006-12-31                >> NOW to 2006-12-31T23:59:59
 * @P3H                         >> NOW to three hours from now
 * @P1Y90D                      >> NOW to one year and 90 days from now
 * 2006-03-05--P1W              >> the week starting Mar 5 2006
 * 2006-01--P3M                 >> 3 months starting Jan 2006

The url is much longer this way, of course. But it's at least semi-intuitive (someone could figure out how to change a view to display different results). And you can use it programatically to create links to views using nearly any date criteria.

Anyway, my plan is to add something similar to the Event Views module.


great work

moshe weitzman's picture

thanks for documentingn all your great work, Karen.

Agreed, fantastic job!

webchick's picture

I couldn't believe that a feature request for "some date to some date" turned into like a date range extravaganza. :) I'm stoked!!

The other thing that gets me excited about this functionality is we're that much closer to being able to replicate event.module's functionality using just Views + CCK. Anyone up for coding a calendar view plug-in? ;)

The calendar view plug in

KarenS's picture

The calendar view plug in almost exists. I have something close working in event views where you can plug any date into a calendar using views. It needs tweaking since cck changed its structure late in August and the method that used to work no longer works, but it's close.


moshe weitzman's picture

thats a tantalizing comment, karens. looking forward to seeing this.

I took a look at this, and I

gordon's picture

I took a look at this, and I was going to do this by just re-theming a list view.

Other than the fact that I believe SQL was complex for views to generate, it would have worked fine.

Really you just need to create a views plugin which will create and populate the calendar based on a field.

Gordon Heydon

Yes this is great. Would it

gordon's picture

Yes this is great.

Would it be possible to apply this to 2 dates (eg. start and end date) in view or is this going to be a limitation of views?

So I want to select any node which the start date is between 2 dates or the end date is between 2 dates, or the start date is less than the earliest date, and the end date is greater then the lastest date?

I think this is pushing it. But I thought I would ask.

again, Great work.

Gordon Heydon

Wow. Not sure yet if this

KarenS's picture

Wow. Not sure yet if this could be done. It's not necessarily a limitation of Views, but might be a limitation in my ability to figure out how to do it :-)

I did have a quick look into

gordon's picture

I did have a quick look into doing this with views, but in the end a developed a custom module which for do it for me.

here it is.

= db_query(db_rewrite_sql("SELECT DISTINCT(n.nid), sd.field_start_date_value as start, ed.field_end_date_value as end FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {node_data_field_start_date} sd ON n.vid = sd.vid LEFT JOIN {node_data_field_end_date} ed ON n.vid = ed.vid WHERE n.type = 'content_notice' AND tn.tid IN (292, 294) AND ((sd.field_start_date_value >= %d AND sd.field_start_date_value <= %d) OR (ed.field_end_date_value >= %d AND ed.field_end_date_value <= %d) OR (sd.field_start_date_value <= %d AND ed.field_end_date_value >= %d))"), $start, $end, $start, $end, $start, $end);

And this works quite will.

With views I would like to see this as an argument, then you could calculate the start and end dates in a snippet of php, and populate the args.

But I am not sure if views could contruct the SQL like this.


Gordon Heydon

i think i submitted a patch that does this

dado's picture

it was on an older version of event_views module

It worked for me but i did not test the end date arguments i think. accepted input of any date format parseable by PHP function strtotime()

@dado - looks like I let

KarenS's picture

@dado - looks like I let your patch get buried. Sorry! I'll try to get to it soon.


aexl's picture

no PHP date arguments?

jenlampton's picture

I may have an unusual use case, but it seems like what I want to do shouldn't be impossible. I'm using views, panels, calendar, and date. I want a panel to contain 6 one-month views of my calendar: now, next month, 2 months from now, etc. I've tried every combination I can think of, and The only way I can get this working is by hard-coding month values into each panel argument input, and manually updating all 6 panels every month. Not cool!

1st attempt. I would like to have one calendar content-pane view that accepts date arguments, and I could then add the same pane 6 times with 6 different arguments on my panel. Unfortunately, the panel argument entry form won't accept PHP, so I can't pass date(now+1m) on pane config.

2nd attempt. Create six content-panes that default to different months. Nope, all calendar views are REQUIRED to default to 'now'.

3rd attempt. Create six content-panes, hijack and override the argument handling code for each one.

Is there no better way to do this via the UI?

no php

droweski's picture

Hello jenlampton, I have the same problem. Did you ever discover a solution.

If you'd like to set a

grasmash's picture

If you'd like to set a default time period for a few different calendar views:

  • Edit the Calendar View
  • Add a display
  • Add the necessary date argument
    • Set " Action to take if argument is not present" to "Provide default argument"
    • Select whatever you'd like the default to be. Among the options is "PHP Code" which will let you set the argument dynamically.