php - Filter posts by date with ACF and group them by month

one text

I am creating a Agenda for a website. The goal is to display posts, filtered by an ACF date field and group them by month. Also wrapping them in a div and the name of the month displayed. The Result should be something like this:

Nov:
-post set in november
-post set in november

Dec:
-post set in december
-post set in december

I've managed to get this thus far, but the problem is that there is an empty div being generated.

enter image description here

There is an empty <div class="month-group">

I've already tried closing the div after the endforeach and various other points, in different ways, but nothing really works and breaks the purpose of the code.

So this is the code I am using

<div class="posts">

                <?php

                    $today = date('Ymd');

                    $args = array(
                        'post_type'     => 'agenda',
                        'meta_key' => 'start_date',
                        'orderby' => 'meta_value_num',
                        'order' => 'ASC',
                        'posts_per_page' => 4,
                        'meta_query' => array(
                            'key'   => 'start_date' , 
                            'compare' => '>=',
                            'value'   => $today,
                            'type'        => 'date'
                        )
                    );

                    $shows = get_posts( $args );

                        $current_header = ''; ?>

                        <div class="month-group">
                        <h4 class="month"><?php date_i18n('M', strtotime($date)); ?></h4>

                        <?php

                        foreach ($shows as $post ) : setup_postdata($post);

                            // get raw date
                            $date = get_field('start_date', false, false);

                            $temp_header = date_i18n('F Y', strtotime($date));

                            if ( $temp_header != $current_header ) {
                                $current_header = $temp_header;
                                echo "</div>";
                                echo " <div class=\"month-group\">";
                                echo " <h4 class=\"month\">" . date_i18n('M', strtotime($date)) . "</h4>";
                            } ?>

                            <div class="post">
                                <h4><?php the_title(); ?></h4>
                                <span><?php the_field('start_date'); ?></span>
                            </div>

                    <?php
                        endforeach; 
                        wp_reset_postdata();
                    ?>

                </div>

            </div>

So does anyone know, how I can close it correctly?

Thank you!

Source