function RulesDateOffsetProcessor::applyOffset

Intelligently applies the given date offset in seconds.

Intelligently apply duration values > 1 day, i.e. convert the duration to its biggest possible unit (months, days) and apply it to the date with the given unit. That's necessary as the number of days in a month differs, as well as the number of hours for a day (on DST changes).

2 calls to RulesDateOffsetProcessor::applyOffset()
RulesDateOffsetProcessor::process in modules/rules_core.eval.inc
Overrides RulesDataProcessor::process().
rules_action_data_calc in modules/data.eval.inc
Action: Calculate a value.

File

modules/rules_core.eval.inc, line 187

Class

RulesDateOffsetProcessor
A data processor for applying date offsets.

Code

public static function applyOffset($timestamp, $offset) {
    if (abs($offset) >= 86400) {
        // Get the days out of the seconds.
        $days = intval($offset / 86400);
        $sec = $offset % 86400;
        // Get the months out of the number of days.
        $months = intval($days / 30);
        $days = $days % 30;
        // Apply the offset using the DateTime::modify and convert it back to a
        // timestamp.
        $date = date_create("@{$timestamp}");
        $date->modify("{$months} months {$days} days {$sec} seconds");
        return $date->format('U');
    }
    else {
        return $timestamp + $offset;
    }
}