Filter page by date from structure. Only show upcoming dates from structure field

#1

Trying to figure out how to filter dates from a structure-field and only show dates that are in the future.
Filterering from the pages works like a charm, but it reveals all the input from the structurefields.
Went trough a couple of other topics. Cant really make heads or tails.

<?php


$items = page('archief')->children()->filter(function($child) {
  $structure = $child->playtime()->toStructure();
  return $structure->filter(function($item) {
    $today = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
    return $item->eventdate()->toDate() > $today;
  })->count();
});


?>

<?php foreach ($items as $event): ?>
  <a href="<?= $event->url() ?>">
    <li class="event-container">
        <ul class="event-flex">

              <?php foreach ($event->playtime()->toStructure() as $date): ?>
            <ul class="playtime">
              <li>
                <h1><?= $date->eventdate()->toDate('%a') ?></h1>
                <h1><?= $date->eventdate()->toDate('%d') ?></h1>
                <h1><?= $date->eventdate()->toDate('%B') ?></h1>
                <h1><?= $date->time() ?></h1>
                <h4><?= $date->eventtype() ?></h4>
              </li>
              <li><span class="ticket-button">TICKET</span></li>
            </ul>

              <?php endforeach ?>
            </figcaption>

    <?php endforeach ?>
0 Likes

#2

If I get this right, you want to do two things:

  1. You want to filter all children of archief and only fetch those that contain events with a date in the future within their structure field item. That what you achieve with the first filter.

  2. The when you display the events from the structure field itself, you also only want to show those in the future. To achieve this, you would have to use a second filter on the structure items in the second foreach loop.

0 Likes

#3

Yes correct on both points!

So how would i achieve that filter on the for each loop? or for what do i need to search?

<?php foreach ($event->playtime()->toStructure() as $date): $date->eventdate()->toDate() == $today; ?>
<li><span class="ticket-button"><?= $date->time() ?></span></li>
<?php endforeach ?>
0 Likes

#4

Something like (not tested)

<?php 
$eventitems = $event->playtime()->toStructure()->filter(function($item) {
  return $item->eventDate()->toDate() >= time();
});
foreach ($eventitems as $date): ?>
<li><span class="ticket-button"><?= $date->time() ?></span></li>
<?php endforeach ?>
0 Likes

#5

Thanks, i swopped time() with $today and it seems to work.

<?php 
$eventitems = $event->playtime()->toStructure()->filter(function($item) {
$today = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
return $item->eventDate()->toDate() == $today;
});
foreach ($eventitems as $date): ?>
0 Likes