Convert Raw PHP to php and html

I was wondering if someone could show me how to convert this Raw PHP code so I can break out of php and style the code a little easier.
I’ve been trying to figure it out for a few hours but can’t seem to get the same results as the raw php.

Here is the RAW php code:

<?php
  include_once($_SERVER['DOCUMENT_ROOT']."/includes/database.php");

    $result = array();
    $stmt = $db->prepare("SELECT `id`,`date`,`title` FROM `htp_news` ORDER BY date DESC");
      if ($stmt->execute()) {
        while ($article = $stmt->fetch(PDO::FETCH_OBJ)) {
          $year = date("Y", strtotime($article->date));
          $month = date("F", strtotime($article->date));
         $result[$year][$month][] = $article;
       }

      foreach ($result as $year => $data) {
        echo "<h1>" . $year . "</h1>";
        foreach ($data as $month => $articles) {
          echo "<h2>" . $month . "</h2>";
          echo "<ul>";
          foreach ($articles as $article) {
             echo "<li><a href='htp-news.php?id=". $article->id ."'>" . date("M d, Y", strtotime($article->date)) . " - " . $article->title . "</a></li>";
          }
          echo "</ul>";
        }
 	 }
 	}
 ?>

And here is my butchered attempt:

<?php
  include_once($_SERVER['DOCUMENT_ROOT']."/includes/database.php");

    $result = array();
    $stmt = $db->prepare("SELECT `id`,`date`,`title` FROM `htp_news` ORDER BY date DESC");
      if ($stmt->execute()) {
        while ($article = $stmt->fetch(PDO::FETCH_OBJ)) {
          $year = date("Y", strtotime($article->date));
          $month = date("F", strtotime($article->date));
         $result[$year][$month][] = $article;
       }
 ?>
 <?php foreach ($result as $year => $data): foreach($data as $month => $articles);?>
 	<h1><?php echo $year ?></h1>
 	<h2><?php echo $month ?></h2>
 		<?php endforeach ?>

  	<ul>
 <?php foreach ($articles as $article):?>
 	
 	<li>
 <?php echo "<a href='htp-news.php?id=". $article->id ."'>" . date("M d, Y", strtotime($article->date)) . " - " . $article->title . "</a>" ?>
 	</li>

 <?php endforeach ?>
 	</ul>
 <?php } ?>

With my attempt I get the following results

2014

February

2013

December

•Dec 27, 2013 - Foyer Divine School
•Dec 19, 2013 - Foyer Divine School
•Dec 19, 2013 - Mireille Senecharle


It looks like this when everything is correct:

2014

April

•Apr 10, 2014 - Test Post Images
•Apr 10, 2014 - Test Post 2
•Apr 10, 2014 - Test New Post 1
•Apr 05, 2014 - test 5
•Apr 05, 2014 - test 6
•Apr 02, 2014 - Test post 1

March

•Mar 13, 2014 - New Post Update test!
•Mar 11, 2014 - TEST post 2
•Mar 11, 2014 - TEST post 3

February

•Feb 07, 2014 - Mireille Senecharle

2013

December

•Dec 27, 2013 - Foyer Divine School
•Dec 19, 2013 - Foyer Divine School
•Dec 19, 2013 - Mireille Senecharle

I appreciate any help I can get. Thank you!

Never mind This worked. I got some help from a buddy. This is a good script to archive your database posts, for a blog or etc…
Hope someone can find it useful.

<?php
  include_once($_SERVER['DOCUMENT_ROOT'] . "/includes/database.php");

  $result = array();
  $stmt = $db->prepare("SELECT `id`,`date`,`title` FROM `htp_news` ORDER BY date DESC");

  if ($stmt->execute()) {
    while ($article = $stmt->fetch(PDO::FETCH_OBJ)) {
      $year = date("Y", strtotime($article->date));
     $month = date("F", strtotime($article->date));
     $article->date = date("M d, Y", strtotime($article->date));
     $result[$year][$month][] = $article;
   }

   foreach ($result as $year => $data): ?>
     <h1><?= $year ?></h1>
     <?php foreach ($data as $month => $articles): ?>
       <h2><?= $month ?></h2>
       <ul>
         <?php foreach ($articles as $article): ?>
           <li>
             <a href="htp-news.php?id=<?= $article->id ?>">
               <?= $article->date ?> - <?= $article->title ?>
             </a>
           </li>
         <?php endforeach ?>
       </ul>
     <?php endforeach ?>
   <?php endforeach ?>
 <?php } ?>