Multiple date fields in 1 markdown file?

How can I use multiple date fields in 1 markdown file?

I would like to build an event section to a website. Each event has a start-date and an end-date. I can easily add those fields as such:

----
startdate: 04/20/2016
----
enddate: 04/23/2016
----

But then I seem to be unable to use the filterby and sortby function on the collection ?? It seems like it doesn’t filter/sort at all…

$events = $pages->find("events")->children()->sortBy("startdate", "asc");
or
$events = $pages->find("events")->children()->filterby("enddate", '>=', time());

I did a quick test with renaming the field to “date”, and then it seems to be working? Is it somehow “hardcoded” that date fields should be named “date”?

What am I doing wrong?

Thanks!

For the filter function, you can use a filter with callback like this:

$events = $pages->find('events')->children()->filter(function($child) {
      return strtotime($child->enddate()) >= time();
});

As for the sorting bit, that should work without any problems even if the date field has another name.

Have you entered the dates via the panel? I think the format should be yyyy-mm-dd.

And when printing the dates in the template, you can use this code:

echo $page->date('Y-m-d', 'startdate');

where the first param is the date format and the second one is the field name to use.
This is some kind of helper for printing date values.

1 Like

Thank you both @texnixe & @lukasbestle, your callback tip worked like a charm, and I upon a quick test I think the sorting should be fixed too now upon changing the format to yyy-mm-dd.

You guys make kirby great. This kind of support on a weekday at 10.30pm is simply outstanding.

Thanks!

2 Likes

Hi @bvdputte!

This could also be interesting for you:

Saludos,

Funkybrotha