title: Order items by n number of dates and fetch the closest to today
body: <p>I have a website that displays events; Each event can have x number of dates (no limits).
  I need to display on the main page the event that is closest to today's date.</p>
  
  <p>This should be a no-brainer, but it's not working for some reason. Any help would be appreciated.</p>
  
  <p>So here is my function that selects the main event. Events have been previously fetched from the DB, with linked dates stored under the key "dates". They are passed to the function through the argument "$events".
  Each date comes as an (int)unix date.</p>
  
  <pre><code>function getMainEvent(&amp;$events){
      $today = time();
      $currentEvent = end($events); //selecting the last entered event (most likely to be the main event)
      $currentEventLast = max($currentEvent['dates']); //selecting the highest date linked to that event
      foreach($events as $k=&gt;&amp;$event){ //looping through events
          $date = min($event['dates']); //selecting the lowest date for the current event
          if($date&gt;$today &amp;&amp; $date&lt;$currentEventLast){
          //if $date is higher than today (so it's not in the past), but lower than main event's date...
              $currentEvent = &amp;$event; //make that event the main event
              $currentEventLast = max($currentEvent['dates']); //select the latest date of that event
          }
      }
      return $currentEvent;
  }
  </code></pre>