Issue with filtering page collection by date

Hi all, I’m having some issues with the filterBy() method trying to get the pages that are since a specific date.

In my controller:

$recent = $page->children()->listed()->flip()->filterBy('date', '>', strtotime('2009-12-31'));

  // function that returns the formatted date
  $pageyear = function($p) {
  return $p->date()->toDate('Y');
  };
  
  // group items using $callback
  $RecentYears = $recent->group($pageyear);

  
  // allow filtering to the URL
  if($year = param('year')) {
  $articles = $page->children()->listed()->flip()->filter(function($p) use($year) {
    return $p->date()->toDate('Y') === $year;
  	});
  }

and then in my template:

<?php foreach ($RecentYears as $year => $YearList): ?>
   <h2><a href="<?= page('calendar')->url() . '/year:' . $year ?>"><?= $year ?></a></h2>	
<?php endforeach ?>

Which works exactly as expected IF I remove ->filterBy('date', '>', strtotime('2009-12-31')) but of course gives me all of the years before 2010 as well.

I can’t seem to make any sense of why this would be happening. Many many thanks for any assistance

The date field doesn’t return a Unix timestamp anymore:

->filter(function ($child) {
    return $child->date()->toDate() > strtotime('2009-12-31') ;
  });

Aha. I thought that might be the case, but I was referencing this:

https://getkirby.com/docs/cookbook/search/filtering#fun-with-filtering-by-date and it seemed like it would still work that way.

This is working perfectly now. Thank you!!

Looks like that bit of the docs was overlooked, needs fixing.

Okay great. Thanks again. Lots of dates with my site, so this is fixing a million things now for me. Thank you!

In Kirby 2, the date field was an exception to other fields, which resulted in all sorts of issues. So we made this a standard field that you now have to call with the toDate() method to turn it into a date, just like any other date fields that are not called date.

Right that makes a lot of sense, I had sorted that out for everywhere I’m displaying the date, and it definitely makes it clearer (I’ve got a lot of date spans so, multiple date fields)

In trying to get this sort of filtering to work to filter a date span I used to be able to chain filterBy()s.

If I wanted to get all the pages with dates between 2010 and 2015 say, what would that look like?

->filter(function ($child) {
    return $child->date()->toDate() > strtotime('2009-12-31')  && $child->date()->toDate()  < 
strtotime('2016-01-01');
 });

Ah, of course. Thank you again for all your help!