Hello. I need a hand with what I think is a custom filter function. I’d appreciate a little help. Let me explain the issue.
I have a page with a field named ‘date’. When looking at the text file the value for ‘date’ is something like ‘2017-04-18’, but when echoing the ‘date’ value it’s something like ‘1492560000’.
I would like to use the filterby method like the following but it doesn’t work because of the format of date is wrong.
$page->children()->filterby('date', '2017-04-18');
I can’t do this (obviously)…
$page->children()->filterby(date('Y-m-d'), '2017-04-18');
… but I do need to get that date in the ‘Y-m-d’ format so it can potentially match the value I’m trying to match.
Any ideas?
$page->children()->filterby('date', strtotime('2017-04-18'));
There are also some examples in the Filtering compendium (maybe useful for future use)
Thanks for the reply. I didn’t explain this as well as I should have. Sorry.
Imagine this…
$target_date = '2017-04-18';
$target_date = strtotime($target_date);
$page->children()->filterby('date', $target_date);
That doesn’t work because my ‘date’ field is really spittin’ out something like 1492560000. Consider this post.
The date method returns a unix timestamp (this “something like 1492560000”) ; strtotime()
converts your date string into a unix timestamp. I don’t see why this shouldn’t work for you. It has always worked for me until now.
The post you linked to is about filtering by year, not about filtering by a particular date.
Edit:
But if you don’t like it and if it makes you more happy to use a custom filter, here you go:
$target_date = '2017-04-18';
$filteredPages = $page->children()->filter(function($child) use($target_date) {
return $child->date('Y-m-d') == $target_date;
});
After all, we want see our users happy . But trust me, the first example works.
Turns out my $target_date variable needed the strtotime() function applied to it - something like this…
$page->children()->filterby('date', strtotime($target_date()));
So you were right from the git go. I’m actually using the calendar library and it’s not a string and it’s not even $target_date. It’s $day->day(). So there’s was a higher level of complexity I was not mentioning for the sake of simplicity, but I should have.
Sonja, thank you for be so generous with your time (over and over again).