Layout builder advanced display code grouping by month and year

<?php while ( have_posts() ) : the_post(); ?>  

    <?php
    $month = get_the_date( 'Y F' );
    if ( empty( $month ) || $month != $last_month ) {
        echo '<h3 class="month">' . $month . '</h3>';
    }
    $last_month = $month;
    ?>
 
    <div class="news_article">
         <h4><?php the_title(); ?></h4>
         <p><?php echo get_the_date(); ?> by <?php the_author();?></p>
    </div>

<?php endwhile; ?>