title: php mongo - cursor is not sorting dates properly
body: <p>I have the following code to query a mongo database for history data: </p>
  
  <pre><code>    function get_location_history($location)
      {
              global $m, $db;
              $collection = $db-&gt;location_ppg_history;
              try{
                      $query = array('tname'=&gt;$location);
                      $cursor = $collection-&gt;find($query)-&gt;sort(['ppg_run_date'=&gt;-1]);
                      return $cursor;
              } catch(MongoCursorException $e) {
                      return false;
              }
      }
  </code></pre>
  
  <p>This is the call to the above method ... and the logic to parse the results: </p>
  
  <pre><code>  $status = get_location_history($tname);   
    foreach ($status as $id=&gt;$value) {
       ..... echo ("&lt;td&gt;".$value['ppg_run_date']."&lt;/td&gt;");
    }
  </code></pre>
  
  <p>The output on the page looks like this: (just a subset of output to give you an idea)</p>
  
  <pre><code>    September 27 2016 16:08:47  
      September 19 2016 9:31:40   
      September 12 2016 9:23:43   
      September 05 2016 9:34:44   
      October 10 2016 9:35:33 
      October 03 2016 9:41:54 
  </code></pre>
  
  <p>Within each month, as you can see, it's printing the dates in descending order, which is what I want.. But I can't figure out why September data is returned before October. </p>
  
  <p>Any suggestions? </p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>This is what my data in the database looks like: </p>
  
  <pre><code>{
      "_id" : ObjectId("57b2283aea89161b704c8980"),
      "tname" : "t144f",
      "ppg_run_date" : "August 15 2016 15:16:17",
      "ppg_log_date" : "August 15 2016 20:38:17",
      "type" : "locstatus",
      "customized" : true
  } 
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>This is the PHP code that creates the date field... </p>
  
  <pre><code>  $GLOBALS['gstrPPGRunDate'] = date('F d Y G:i:s', strtotime($matches[2]) );
  </code></pre>
  
  <p>Eventually, this Global variable is inserted into an array of data ($status) that represents an individual document.  That document in turn is added to the mongo collection. </p>
  
  <pre><code>try {
          $cursor = $collection-&gt;update(array('sdomain'=&gt;$status['sdomain']) , array('$set'=&gt;$status), array('upsert'=&gt;true) );
  
          return true;
      } catch(MongoCursorException $e) {            
          return false;
      }
  </code></pre>
  
  <p>How do I change this to ensure that this field is treated as a mongo date instead of a string? I think I will try this:  <a href="http://php.net/manual/en/class.mongodate.php" rel="nofollow">http://php.net/manual/en/class.mongodate.php</a>  to replace the logic that currently creates the global variable.</p>