9

Drupal 7, Views 3

I've got several views configured which display data over many years. The data is filtered by Year using a date field as filter criteria. For each year I have attachment views that I need to filter further by week. Rather than determining the exact dates for the weeks of each year and inputting them manually as filter criteria, I'm wondering if there is any way to add a filter criterion to filter by week number, i.e. week 1 thru week 52. That way the only date that needs to be set is the year for each view, and the rest happens automatically according to week number.

I am aggregating a lot of data by year and week in a table view where each column is an additional view attachment and trying to avoid having to manually enter the filters for week dates per year.

Does anyone have any advice/hints for me on this? Thanks very much.

Here is the code I am currently playing with:

function x_week_start($date) {
    $ts = strtotime($date);
    $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
    return date('Y-m-d', $start); 
}

function x_week_end($date) {
    $ts = strtotime($date);
    $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
    return date('Y-m-d', strtotime('next saturday', $start)); 
}

$date = '2013-01-14';
$start = x_week_start($date);
$end = x_week_end($date);
$inquiry_date = date('Y-m-d', $data->field_field_inquiry_date[0]['raw']['value']);

if ($inquiry_date <= $start) {return FALSE;}

I should add that the field_inquiry_date field is a unix timestamp.

3

2 Answers 2

2

Here's a trick: create a new date format by navigating to Admin > Configuration > Regional and Language > Date and Time and clicking on the Formats tab (admin/config/regional/date-time/formats).

Set it to W (that's uppercase W), and it will give you week number, which you can use with any date.

That way you can include the date field directly in views, set the W filter and use the aggregation, filter, etc without any additional coding.

1

https://www.drupal.org/project/views_date_format_sql might help you in grouping the table data with date formatters of SQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.