hook_civicrm_relativeDate¶
Summary¶
Allows extensions to define custom relative date filters by returning an array with from
and to
date values, enabling flexible filtering beyond the built-in formats.
Definition¶
hook_civicrm_relativeDate($filter)
Parameters¶
- string $filter the relative date filter that have been added to the option group
relative_date_filters
Returns¶
- array An associative array with
from
andto
keys representing the start and end dates.
Examples¶
Here is a simple example, assuming your extension is called myextension
and that the values ccourse
and pcourse
have been added to the option group relative_date_filters
:
/**
* Implements hook_civicrm_relativeDate().
*
* Allows extensions to define custom relative date filters by returning a date range.
*
* @param string $filter
* The relative date filter have been added to the option group `relative_date_filters`
*/
function myextension_civicrm_relativeDate($filter) {
$dates = [];
if ($filter == 'ccourse') {
$dates['from'] = (new DateTime('September 1st -1 year'))->format('Ymd');
$dates['to'] = (new DateTime('August 31st'))->format('Ymd');
}
if ($filter == 'pcourse') {
$dates['from'] = (new DateTime('September 1st -2 year'))->format('Ymd');
$dates['to'] = (new DateTime('August 31st -1 year'))->format('Ymd');
}
return $dates;
}