Put child in foreach loop in another parent if date is past

In my Page I have an “events” section with dated events. I have two divs for status “upcoming” and “past” events:

<div class="upcoming">
  <article class="event">1</article>
  <article class="event">2</article>
  <article class="event">3</article>
</div>

<div class="past">
  <article class="event">4</article>
  <article class="event">5</article>
</div>

What I want is to put them automatically to the “past” div as soon as the date is over.
I tried something like this:

<?php $events =  $page->children()->listed(); 

foreach ($events as $event) :

  $date = new DateTime($event->date());
  $now = new DateTime();

  if ($date < $now) : ?>

    <div class="past">
      <article class="event">...</article>
    </div>

  <?php else : ?>

    <div class="upcoming">
      <article class="event">...</article>
    </div>

<?php endforeach ?>

So, since the parent “upcoming” and “past” divs are part of the foreach loop, I get a parent div for every single event.

Is it possible to check the date value of each event before the loop and then separate the results into two different foreach loops?

You have to filter your collection by past and upcoming first, then create a div for each group and loop through the two collections inside each div.