Filter pages by datetime field

Hi again,
I’m having difficulties achieving filterBy with a datetime field. I’ve seen the tutorials about filtering:

// fetch children with a date in the past
$items = $page->children()->filterBy('date', '<', time());

I don’t know where date is taken from, but I have this in my blueprints:

  activefrom:
    label: Activation Date
    type:  datetime

  activeuntil:
    label: Active END Date
    type:  datetime

And therefore I want to filter the pages where now is between those two days, a common situation. I’m using

$page->children()->filterBy('activefrom', '<', time());

But I’m getting Object of class Field could not be converted to int

in kirby\vendor\getkirby\toolkit\lib\collection.php,

specifically if(collection::extractValue($item, $field) < $value) continue;


My value is Activefrom: 2017-02-02 09:00:00

If I try to dump the value, it’s a real field with all the properties. Converting it to string actually returns 2017-02-02 09:00:00, BUT the < operator forces is to be converted to int, which just fails.


Am I missing something?

1 Like

Check out this section of the filtering cookbook recipe: https://getkirby.com/docs/cookbook/filtering#fun-with-filtering-by-date

You can’t use filterBy() in this case, but need to use the filter() method with a callback.

@texnixe You’re like a walking Kirby encyclopedia! :slight_smile:
Thanks a lot! I guess it would be nice if these things are mentioned in the docs, especially here: https://getkirby.com/docs/cheatsheet/pages/filter-by

It’s said “Filters the collection by any field and value and with a set of filtering operators”, and there’s an example with date, so any one would think this would work with any date field.

Thanks again!

In general, this is true, but the date field is a bit special, that’s why it is more complicated in this case.