I had an interesting request on a Drupal site that I'm working on. I've created a view with a list of names that had a significant date and some other information. My original understanding was that we needed the list grouped by year of the significant date listed in descending order by the significant date. This was easily done using Views 2 right out of the box.
There was only one specific requirement that made this a little difficult. The significant date did not run on a standard January to December calendar but was seasonal going from June 1st to May 31st of the next year. Jan 1st to May 31st are actually going to be on the prior year.
This was one of those rare moments in Drupal that I actually needed to put some PHP together to get what I wanted. In this case, I made use of Views Custom Field, (http://drupal.org/project/views_customfield) to put my custom code into the view.
Below is the code I used, first I assigned the value of my date field to a variable named $my_date, then I used the format_date function from DATE API to assign two variables, one is a month-day variable used to filter what I needed to modify and the other a four digit year, which is the varible that will be modified.
Basically, from there, anything that is less than or equal to 530, which will be all months and dates from Jan 1st to May 30th will take the year variable and subtract it by one then echo it. All others will just echo the year as it was.
<?php
$my_date = strtotime($data->node_data_field_lunker_date_field_lunker_date_value);
$lunker_month_day = format_date($my_date, 'custom', 'nd');
$lunker_year = format_date($my_date, 'custom', 'Y');
if ($lunker_month_day <= 530)
{
$lunker_year = $lunker_year - 1;
}
echo $lunker_year;
?>
From there, in the view, I excluded the display of that field; then used it as the grouping field in the table.
