Events - Startdate/Enddate (Enddate - Filter)

Hi Kirby-Fellaz!

I have created an Events-Overview - Site
Each Event-entry has a Start- and Enddate.

Now I want to hide the Events when the Enddate
has passed. This is my Code:

Blueprint

title: Events Element
pages: false
files: true
  fields:
    caption:
      label: Caption
      type: textarea
fields:
  Main Content Meta:
    label: Main Content
    type: headline
  title:
    label: Title
    type:  text
  main_title:
    label: Main Title
    type:  text
  category:
    label: Category
    type: tags
  type:
    label: Type
    type: text
  startdate:
    label: Start Date
    type:  date
    format: MM/DD/YYYY
    width: 1/2
  enddate:
    label: End Date
    type:  date
    format: MM/DD/YYYY
    width: 1/2

Template

	<div class="container">
		<div class="row">

			<?php foreach ($site->find('termine')->children()->sortBy('startdate', 'asc') as $element) : ?>

			<?php
				$eventStartdate	= $element->date('%e. %b. %Y', 'startdate');
				$eventEnddate	= $element->date('%e. %b. %Y', 'enddate');
			?>

			<div class="col-xs-12 col-sm-6 col-md-4">
				<div class="panel panel-default events">
					<div class="panel-body">
						<div class="tri-category"><?php echo $element->category() ?></div><br />

						<div><?php echo $eventStartdate ?>
						<?php if ($eventStartdate != $eventEnddate): ?>
						<?php echo ' - ' . $eventEnddate; ?>
						<?php endif; ?>
						</div>

						<div class="tri-type">
						<?php echo $element->type() ?>
						</div>

						<h3><?php echo $element->main_title() ?></h3>
						<div><?php echo $element->lead() ?></div>

						<a class="tri-more-button" href="<?php echo $element->url() ?>" title="<?php echo $element->main_title() ?>">mehr</a>
					</div><!-- /.panel-body -->
				</div><!-- /.panel panel-default events -->
			</div><!-- /.col-xs-12 -->
			<?php endforeach; ?>

		</div><!-- /.row -->
	</div><!-- /.container -->

I don’t know how to set the Filter.
Your help is greatly appreciated.

Saludos, Funkybrotha

ps. I tried this: http://getkirby.com/docs/cheatsheet/pages/filterBy
but as I renamed date to startdate + enddate my code should
look a bit different, i think.

You should be able to use filterBy() on your collection like this:

$site->find('termine')->children()->filterBy('enddate', '>', time())->sortBy('startdate', 'asc') as $element) : ?>

Doesn’t it work?

Hi @texnixe!

Thank you!
No… like this i get several errors.

I have to declare my startdate/enddate like this:
<?php $eventStartdate = $element->date('%e. %b. %Y', 'startdate'); $eventEnddate = $element->date('%e. %b. %Y', 'enddate'); ?>
I don’t know how to declare them before the foreeach loop.

Saludos, Funkybrotha

My guess:
in the filter use e.g.

$element->startdate()

to fetch the date of the field “startdate” and to calculate with or to compare it.

If you want to echo the date in a readable format use

$eventStartdate

Good luck!

Hi @anon77445132!

Thanx for your help.

With this code i can output the dates in a nice
readable format:
<?php $eventStartdate = $element->date('%e. %b. %Y', 'startdate'); $eventEnddate = $element->date('%e. %b. %Y', 'enddate'); ?>

For example outputs: 1. Feb. 2016 - 14. Mär. 2016

But this declaration happens after i define $element.
I need to make this before the foreach - loop.

Saludos, Funkybrotha

ps. The code works like posted in my first post.
I’m just missing the ->filterby… enddate - part.

Oh, I see, then you have to use a custom filter:

$site->find('termine')->children()->filter(function($child) {
      return strtotime($child->enddate()) > time();
});

Hi @texnixe!

Thank you for your efforts!
With your Code - Snippet I don’t get any value back. ;-(

Maybe this is just to complicated for me. :wink:

I don’t know where to put that in my Code:

<?php foreach ($site->find('termine')->children()->sortBy('startdate', 'asc') as $element) : ?>

			<?php
				$eventStartdate	= $element->date('%e. %b. %Y', 'startdate');
				$eventEnddate	= $element->date('%e. %b. %Y', 'enddate');
			?>

			<div class="col-xs-12 col-sm-6 col-md-4">
				<div class="panel panel-default events">
					<div class="panel-body">

						<div class="tri-category"><?php echo $element->category() ?></div><br />

						<div><?php echo $eventStartdate ?>
						<?php if ($eventStartdate != $eventEnddate): ?>
						<?php echo ' - ' . $eventEnddate; ?>
						<?php endif; ?>
						</div>

						<div class="tri-type">
						<?php echo $element->type() ?>
						</div>

						<h3><?php echo $element->main_title() ?></h3>
						<div><?php echo $element->lead() ?></div>

						<a class="tri-more-button" href="<?php echo $element->url() ?>" title="<?php echo $element->main_title() ?>">mehr</a>
					</div><!-- /.panel-body -->
				</div><!-- /.panel panel-default events -->
			</div><!-- /.col-xs-12 -->
			<?php endforeach; ?>

Saludos, Funkybrotha

<?php
$elements = $site->find('termine')->children()->filter(function($child) {
      return strtotime($child->enddate()) > time();
});
?>
<div class="container">
  <div class="row">
    <?php foreach ($elements->sortBy('startdate', 'asc') as $element) : ?>
      <?php
        $eventStartdate	= $element->date('%e. %b. %Y', 'startdate');
	$eventEnddate	= $element->date('%e. %b. %Y', 'enddate');
      ?>
        <div class="col-xs-12 col-sm-6 col-md-4">
          <div class="panel panel-default events">
            <div class="panel-body">
              <div class="tri-category"><?php echo $element->category() ?></div>
              <div><?php echo $eventStartdate ?>
	      <?php if ($eventStartdate != $eventEnddate): ?>
	        <?php echo ' - ' . $eventEnddate; ?>
	      <?php endif; ?>
	      </div>
              <div class="tri-type">
		<?php echo $element->type() ?>
	     </div>
		<h3><?php echo $element->main_title() ?></h3>
		<div><?php echo $element->lead() ?></div>
                  <a class="tri-more-button" href="<?php echo $element->url() ?>" title="<?php echo $element->main_title() ?>">mehr</a>
	    </div><!-- /.panel-body -->
	</div><!-- /.panel panel-default events -->
     </div><!-- /.col-xs-12 -->
  <?php endforeach; ?>
  </div><!-- /.row -->
</div><!-- /.container -->

Hey @texnixe!

Thanx a lot! It works!!! Yeah!!!
I must say i’m really impressed with the quality of the support in this forum.

One small add. question:
How can I add one day to the Filter?

If I create an Event from yesterday to today…
It disappears already today. But I want to show the Event till the Enddate has passed.
Is it possible to add like +1 day or 24h?

Saludos, Funkybrotha

ps. Hey @texnixe! I wish you happy holidays! :grinning:

You’re welcome!

$elements = $site->find('termine')->children()->filter(function($child) {
      return strtotime($child->enddate()) >= time();
});

Happy holidays to you, too :smiley:

Hi @texnixe!

Thanx again for your help!

‘>=’ does not work.
I’ve read something that the returned value
by time() is minutes and not a day - value?
Do I need to convert that to something
different?

Saludos, Funkybrotha

Then try this:

$projects = page('my-projects')->children()->filter(function($child) {
      return strtotime('+1 day', strtotime($child->enddate())) > time();
});

Hi @texnixe!

YESSS!!! IT WORKS!!!

A BIG THANK YOU!!!

:grinning:

Saludos, Funkybrotha