# Create a collection of dates based on current day

**URL:** <https://forum.getkirby.com/t/create-a-collection-of-dates-based-on-current-day/23018>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 29, 2021, 12:57pm UTC](https://forum.getkirby.com/t/create-a-collection-of-dates-based-on-current-day/23018 "2021-07-29T12:57:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tacotac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tacotac/32/14065_2.png) [@tacotac](https://forum.getkirby.com/u/tacotac)\
**Post date:** [July 29, 2021, 12:57pm UTC](https://forum.getkirby.com/t/create-a-collection-of-dates-based-on-current-day/23018/1 "2021-07-29T12:57:03Z")

</div>

Hi dear community,

I’ve recently created a sort of calendar that displays only dates extracted from a collection of upcoming events. Everything works as expected, but my client finally wants to show 90 days (3 month) in this calendar, even if no events are scheduled 🤯

Currently, dates are stored in each event page in a structure field that looks like:

```auto
dates:
  type: structure
  fields:
    day:
      type: date
      display: DD/MM/YYYY
    hour:
      type: time
      display: H:mm

```

And all dates of all events are merged in a collection this way (which is later grouped, filtered, etc):

```auto
$alldates = new Collection();
foreach($kirby->collection('events') as $event){
  $alldates->add($event->dates()->toStructure()->filter(function ($item) {
    return $item->hour()->isNotEmpty() && $item->day()->toDate('%F') >= date('Y-m-d');
  }));
}

```

Do you know how I can replace the foreach loop above so the **$alldates** collection contains 90 days starting from today, without changing how dates are currently stored/structured?

Thank you all for your help 🙏

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [July 29, 2021, 2:26pm UTC](https://forum.getkirby.com/t/create-a-collection-of-dates-based-on-current-day/23018/2 "2021-07-29T14:26:35Z")

</div>

You can use the DateTime class, in particular DateInterval to create a 90 day period. The you can check for each day if events exist.

[https://www.php.net/manual/en/class.dateinterval.php](https://www.php.net/manual/en/class.dateinterval.php)

How useful that is to print days without events (if you don’t print something that actually resembles a calendar)

---

<div class="post-metadata">

**Author:** ![tacotac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tacotac/32/14065_2.png) [@tacotac](https://forum.getkirby.com/u/tacotac)\
**Post date:** [July 29, 2021, 2:41pm UTC](https://forum.getkirby.com/t/create-a-collection-of-dates-based-on-current-day/23018/3 "2021-07-29T14:41:39Z")

</div>

Hi **pixelijn** , and thanks for your time!

My current logic is as follow:

1. Create a collection of days based on upcoming events
2. Group the days per month
3. Split the days in 4 time slots
4. For each time slot, filter the events collection and display the event(s) scheduled

This is probably not the nicest way to do it but it works. In order to avoid rewriting all my loops/conditions/filters in steps 2, 3, 4, I thought a solution would be to update only the first step, so the **$alldates** collection stores 90 dates starting from today.

Does that make more sense? 😅

---

<div class="post-metadata">

**Author:** ![tacotac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tacotac/32/14065_2.png) [@tacotac](https://forum.getkirby.com/u/tacotac)\
**Post date:** [July 30, 2021, 9:32am UTC](https://forum.getkirby.com/t/create-a-collection-of-dates-based-on-current-day/23018/4 "2021-07-30T09:32:14Z")

</div>

I played around with _dateInterval_ as suggested and here is the final working code, for reference:

```auto
$alldates = new Collection();
$today = new DateTime();
$end = new DateTime();
$end->add(new DateInterval('P3M'));
$period = new DatePeriod($today, DateInterval::createFromDateString('1 day'), $end);
foreach ($period as $id => $day) {
  $seances->add(new Kirby\Cms\StructureObject([
    'id' => $id,
    'content' => [
      'day' => $day->format("Y-m-d"),
      'hour' => ''
    ]
  ]));
}

```

Thanks for your help **pixelijn** 😊
