filterBy for full 12 month


#1

Hi Guys,

is there a way to filter elements for a full year? I want select next birthdays from a member list, birthdays from today on.

$page->filterBy('date', '>', strtotime(date('d.m.Y')))->sortBy('date', 'asc');

with these line I only get a list for the current year, but not for the upcoming. Is there a way to select the ‘next 12 month’?

Thanks


#2

How are the birthdays stored, I assume with the year of birth?


#3

for data protection reason no, i had to replace the year with the current year


#4

Have a look at PHP’s Datetime. class. I think in your. case, you have. to remove the year and only. get the next. 12 months.


#5

Perhaps this is useful… i did a site with events on it that needed to display future events by year… i don’t think it will take much to restrict this to just the next year…

Assuming your birthday field is called birthdate and the birthdays are stored in pages under a parent called “birthdays”…

<?php
$callback = function($p) {
    // return $p->date('F', 'birthdate'); // by month
    return $p->date('Y', 'birthdate'); // by year
};
$collection = $site->find('birthdays')->children();
$filteredBirthdates = $collection->filter(function($child) {return $child->date(null, 'birthdate') > time(); });
$groupedBirthdays = $filteredBirthdates->sortBy('birthdate', 'asc')->group($callback);
?>

#6

@jimbobrjames This will not work if the birthdays have the current year attached to it, but you still. want to. get. those of the next. year, for example, get the birthdays of next April.


#7

Couldn’t you just add 1 to the returned year in the call back? or to the field value?

Actually i think you would need to reset the field value to the current year plus one, so it works in the future. Im assuming the fields are all set to this year, so adding 1 wont work from 2020.


#8

I. think. you wouldn’t have that problem if you ignore the year, that’s what I suggested above.


#9

i will try both ways and come back when i solve the problem or with new questions :smiley:

thanks so far


#10

Hi there,

it works with DateTime::createFromFormat in combination with ->filter() i can select the dates i need now.

Thanks for the hint!
Thomas