Filter by day (not seconds)

Continuing the discussion from filterBy('date') or some such?:


The filterBy() examples from the docs filter items by seconds because time() returns the current time measured in the number of seconds since the Unix Epoch.

So practically every item of the current day is an item of the past.

I wonder how to filter items by day.

Can you help me with the following requirements?

  1. items from today
  2. items from today or later
  3. items from tomorrow or later
  4. items from yesterday or earlier

I think you could start by replace time() with date('Ymd').

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

Now you compare a timestamp with a YYYYMMDD date. This does not work. Of course not.

Is it possible to format the 'date' part with custom date parameters so you can compare it date('Ymd')?

Or do you have any other ideas?

Try this:

$items = $page->children()->('date', '<=', strtotime(date('Y-m-d'))

Thanks!

To make this work I had to change the date in strtotime to strtotime(date('j-n-Y').

(Y-m-d does not work with srtttotime)

// fetch children with from today or later
$items = $page->children()->('date', '=>', strtotime(date('j-n-Y'))
1 Like