Cake\Chronos\Traits\DifferenceTrait::diffFiltered PHP Method

diffFiltered() public method

Get the difference by the given interval using a filter callable
public diffFiltered ( ChronosInterval $ci, callable $callback, Cake\Chronos\ChronosInterface $dt = null, boolean $abs = true ) : integer
$ci Cake\Chronos\ChronosInterval An interval to traverse by
$callback callable The callback to use for filtering.
$dt Cake\Chronos\ChronosInterface The instance to difference from.
$abs boolean Get the absolute of the difference
return integer
    public function diffFiltered(ChronosInterval $ci, callable $callback, ChronosInterface $dt = null, $abs = true)
    {
        $start = $this;
        $end = $dt === null ? static::now($this->tz) : $dt;
        $inverse = false;
        if (defined('HHVM_VERSION')) {
            $start = new DateTimeImmutable($this->toIso8601String());
            $end = new DateTimeImmutable($end->toIso8601String());
        }
        if ($end < $start) {
            $start = $end;
            $end = $this;
            $inverse = true;
        }
        $period = new DatePeriod($start, $ci, $end);
        $vals = array_filter(iterator_to_array($period), function (DateTimeInterface $date) use($callback) {
            return $callback(static::instance($date));
        });
        $diff = count($vals);
        return $inverse && !$abs ? -$diff : $diff;
    }