title: Using PHP for getting difference between values in first and second rows of a sorted query
body: <p>Sorry for the rather long title.  </p>
  
  <p>I have the current table in my database: 
  <img src="https://i.stack.imgur.com/RIuy0.png" alt="Table">  </p>
  
  <p>I then use the following PHP code to display the information on my website:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  
  function time_elapsed_string($datetime, $full = true) {
      $now = new DateTime;
      $ago = new DateTime($datetime);
      $diff = $now-&gt;diff($ago);
      $diff-&gt;w = floor($diff-&gt;d / 7);
      $diff-&gt;d -= $diff-&gt;w * 7;
      $string = array('y' =&gt; 'year','m' =&gt; 'month','w' =&gt; 'week','d' =&gt; 'day','h' =&gt; 'hour','i' =&gt; 'minute','s' =&gt; 'second',);
      foreach ($string as $k =&gt; &amp;$v) {if ($diff-&gt;$k) {$v = $diff-&gt;$k . ' ' . $v . ($diff-&gt;$k &gt; 1 ? 's' : '');} else {unset($string[$k]);}}
      if (!$full) $string = array_slice($string, 0, 1);
   return $string ? implode(', ', $string) . ' ago' : 'just now';
  }
  
  $servername = "localhost";
  $username = "root";
  $password = "";
  
  $conn = new mysqli($servername, $username, $password);
  mysqli_select_db($conn,"ai-database");
  // if ($conn-&gt;connect_error) {die("Connection failed: " . $conn-&gt;connect_error);}
  
  $sql = "SELECT * FROM `steam` ORDER BY `id` DESC LIMIT 1";
  $query = mysqli_query($conn, $sql);
  while($row = mysqli_fetch_assoc($query)){
   $cards = $row['cards'];
   $backgrounds = $row['backgrounds'];
   $emoticons = $row['emoticons'];
   $gifts = $row['gifts'];
   $timestamp = $row['timestamp'];
  }
  
  echo "
      &lt;div class='category'&gt;
        &lt;h2&gt;Steam Collection&lt;/h2&gt;
        &lt;p class='info'&gt;
          Last Updated: &lt;span&gt;" . time_elapsed_string("@" . $timestamp) . "&lt;/span&gt;
        &lt;/p&gt;
        &lt;div class='item'&gt;
          &lt;div class='title'&gt;Cards&lt;/div&gt;
          &lt;div class='stat'&gt;" . number_format($cards) . "&lt;/div&gt;
        &lt;/div&gt;
        &lt;div class='item'&gt;
          &lt;div class='title'&gt;Backgrounds&lt;/div&gt;
          &lt;div class='stat'&gt;" . number_format($backgrounds) . "&lt;/div&gt;
        &lt;/div&gt;
        &lt;div class='item'&gt;
          &lt;div class='title'&gt;Emoticons&lt;/div&gt;
          &lt;div class='stat'&gt;" . number_format($emoticons) . "&lt;/div&gt;
        &lt;/div&gt;
        &lt;div class='item'&gt;
          &lt;div class='title'&gt;Gifts&lt;/div&gt;
          &lt;div class='stat'&gt;" . number_format($gifts) . "&lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
   ";
  
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>After a bit of css, I get something a little like this:  </p>
  
  <p><img src="https://i.stack.imgur.com/Q9znW.png" alt="Display"></p>
  
  <p>I'm wanting to gather the first and second rows after ordering it by id (Desc), Find the difference between the two values and display it on the website.<br>
  I'm hoping the result should look like this:  </p>
  
  <p><img src="https://i.stack.imgur.com/kt0w9.png" alt="My hopes and dreams">  </p>
  
  <p>I've tried using the following, but I feel it looks rather messy and probably bad practice too.  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  
  $sql = "SELECT * FROM `steam` ORDER BY `id` DESC LIMIT 2";
  $query = mysqli_query($conn, $sql);
  $i = 0;
  while($row = mysqli_fetch_assoc($query)){
   if($i==0){
    $cards = $row['cards'];
    $backgrounds = $row['backgrounds'];
    $emoticons = $row['emoticons'];
    $gifts = $row['gifts'];
    $timestamp = $row['timestamp'];
    $i+=1;
   } else {
    $cards2 = $row['cards'];
    $backgrounds2 = $row['backgrounds'];
    $emoticons2 = $row['emoticons'];
    $gifts2 = $row['gifts'];
    $timestamp2 = $row['timestamp'];
   }
  }
  
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Is there a cleaner and more efficient way to do this?</p>