title: Sorting php object by nearest dates
body: <p>I have php object getting by new WP_Query( $query_args ). I need sort that object by nearest date. Every event have start date and end date. If the start date of the event is closer to today, this event should be the first in the output.</p>
  <p>This is my object in $result value.</p>
  <pre><code>            array(4) {
            [0]=&gt;
            object(WP_Post)#3813 (24) {
              [&quot;ID&quot;]=&gt;
              int(9905)
              [&quot;post_author&quot;]=&gt;
              string(2) &quot;31&quot;
              [&quot;post_date&quot;]=&gt;
              string(19) &quot;2020-07-30 21:14:06&quot;
              [&quot;post_date_gmt&quot;]=&gt;
              string(19) &quot;2020-07-30 21:14:06&quot;
              [&quot;post_content&quot;]=&gt;
              string(251) &quot;ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad&quot;
              [&quot;post_title&quot;]=&gt;
              string(20) &quot;Test 3 same location&quot;
              [&quot;post_excerpt&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_status&quot;]=&gt;
              string(7) &quot;publish&quot;
              [&quot;comment_status&quot;]=&gt;
              string(4) &quot;open&quot;
              [&quot;ping_status&quot;]=&gt;
              string(6) &quot;closed&quot;
              [&quot;post_password&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_name&quot;]=&gt;
              string(20) &quot;test-3-same-location&quot;
              [&quot;to_ping&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;pinged&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_modified&quot;]=&gt;
              string(19) &quot;2020-07-30 21:15:32&quot;
              [&quot;post_modified_gmt&quot;]=&gt;
              string(19) &quot;2020-07-30 21:15:32&quot;
              [&quot;post_content_filtered&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_parent&quot;]=&gt;
              int(0)
              [&quot;guid&quot;]=&gt;
              string(62) &quot;https://www.stag.wediscover.club/listing/test-3-same-location/&quot;
              [&quot;menu_order&quot;]=&gt;
              int(0)
              [&quot;post_type&quot;]=&gt;
              string(7) &quot;listing&quot;
              [&quot;post_mime_type&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;comment_count&quot;]=&gt;
              string(1) &quot;0&quot;
              [&quot;filter&quot;]=&gt;
              string(3) &quot;raw&quot;
            }
            [1]=&gt;
            object(WP_Post)#3812 (24) {
              [&quot;ID&quot;]=&gt;
              int(9903)
              [&quot;post_author&quot;]=&gt;
              string(2) &quot;31&quot;
              [&quot;post_date&quot;]=&gt;
              string(19) &quot;2020-07-29 20:32:40&quot;
              [&quot;post_date_gmt&quot;]=&gt;
              string(19) &quot;2020-07-29 20:32:40&quot;
              [&quot;post_content&quot;]=&gt;
              string(167) &quot;Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events&quot;
              [&quot;post_title&quot;]=&gt;
              string(10) &quot;Location 2&quot;
              [&quot;post_excerpt&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_status&quot;]=&gt;
              string(7) &quot;publish&quot;
              [&quot;comment_status&quot;]=&gt;
              string(4) &quot;open&quot;
              [&quot;ping_status&quot;]=&gt;
              string(6) &quot;closed&quot;
              [&quot;post_password&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_name&quot;]=&gt;
              string(10) &quot;location-2&quot;
              [&quot;to_ping&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;pinged&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_modified&quot;]=&gt;
              string(19) &quot;2020-07-30 20:53:08&quot;
              [&quot;post_modified_gmt&quot;]=&gt;
              string(19) &quot;2020-07-30 20:53:08&quot;
              [&quot;post_content_filtered&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_parent&quot;]=&gt;
              int(0)
              [&quot;guid&quot;]=&gt;
              string(52) &quot;https://www.stag.wediscover.club/listing/location-2/&quot;
              [&quot;menu_order&quot;]=&gt;
              int(0)
              [&quot;post_type&quot;]=&gt;
              string(7) &quot;listing&quot;
              [&quot;post_mime_type&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;comment_count&quot;]=&gt;
              string(1) &quot;0&quot;
              [&quot;filter&quot;]=&gt;
              string(3) &quot;raw&quot;
            }
            [2]=&gt;
            object(WP_Post)#3816 (24) {
              [&quot;ID&quot;]=&gt;
              int(9901)
              [&quot;post_author&quot;]=&gt;
              string(2) &quot;31&quot;
              [&quot;post_date&quot;]=&gt;
              string(19) &quot;2020-07-29 20:31:20&quot;
              [&quot;post_date_gmt&quot;]=&gt;
              string(19) &quot;2020-07-29 20:31:20&quot;
              [&quot;post_content&quot;]=&gt;
              string(158) &quot;Location test 1 Location test 1 Location test 1 Location test 1 Location test 1 Location test 1Location test 1 Location test 1 Location test 1 Location test 1&quot;
              [&quot;post_title&quot;]=&gt;
              string(15) &quot;Location test 1&quot;
              [&quot;post_excerpt&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_status&quot;]=&gt;
              string(7) &quot;publish&quot;
              [&quot;comment_status&quot;]=&gt;
              string(4) &quot;open&quot;
              [&quot;ping_status&quot;]=&gt;
              string(6) &quot;closed&quot;
              [&quot;post_password&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_name&quot;]=&gt;
              string(15) &quot;location-test-1&quot;
              [&quot;to_ping&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;pinged&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_modified&quot;]=&gt;
              string(19) &quot;2020-07-30 20:20:56&quot;
              [&quot;post_modified_gmt&quot;]=&gt;
              string(19) &quot;2020-07-30 20:20:56&quot;
              [&quot;post_content_filtered&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_parent&quot;]=&gt;
              int(0)
              [&quot;guid&quot;]=&gt;
              string(57) &quot;https://www.stag.wediscover.club/listing/location-test-1/&quot;
              [&quot;menu_order&quot;]=&gt;
              int(0)
              [&quot;post_type&quot;]=&gt;
              string(7) &quot;listing&quot;
              [&quot;post_mime_type&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;comment_count&quot;]=&gt;
              string(1) &quot;0&quot;
              [&quot;filter&quot;]=&gt;
              string(3) &quot;raw&quot;
            }
            [3]=&gt;
            object(WP_Post)#3817 (24) {
              [&quot;ID&quot;]=&gt;
              int(9747)
              [&quot;post_author&quot;]=&gt;
              string(2) &quot;29&quot;
              [&quot;post_date&quot;]=&gt;
              string(19) &quot;2020-07-02 21:24:09&quot;
              [&quot;post_date_gmt&quot;]=&gt;
              string(19) &quot;2020-07-02 21:24:09&quot;
              [&quot;post_content&quot;]=&gt;
              string(525) &quot;Join our most popular surfing meetup in Los Angeles.
  
          Surfers with all experience levels welcome!
  
          Approximate schedule for the morning:
  
          8.45 – 9.00 Meeting next to Perry’s Cafe
  
          9.00 – 9.30 Surfing
  
          9.30 – 9.45 Coffe / Snacks
  
          9.45 – 10.15 Surfing
  
          10.15 – 10.30 Coffe/ Snacks
  
          10.30 – 11.00 Volleyball
  
          We advise you to bring hot coffee or tea with you to make this morning even more cozier after surfing)
  
          Feel free to contact us if you have any questions.
  
          We can’t wait to see you!&quot;
              [&quot;post_title&quot;]=&gt;
              string(40) &quot;Surfing and Volleyball at Torrance beach&quot;
              [&quot;post_excerpt&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_status&quot;]=&gt;
              string(7) &quot;publish&quot;
              [&quot;comment_status&quot;]=&gt;
              string(4) &quot;open&quot;
              [&quot;ping_status&quot;]=&gt;
              string(6) &quot;closed&quot;
              [&quot;post_password&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_name&quot;]=&gt;
              string(42) &quot;surfing-and-volleyball-at-torrance-beach-4&quot;
              [&quot;to_ping&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;pinged&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_modified&quot;]=&gt;
              string(19) &quot;2020-07-15 23:06:59&quot;
              [&quot;post_modified_gmt&quot;]=&gt;
              string(19) &quot;2020-07-15 23:06:59&quot;
              [&quot;post_content_filtered&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;post_parent&quot;]=&gt;
              int(0)
              [&quot;guid&quot;]=&gt;
              string(84) &quot;https://www.stag.wediscover.club/listing/surfing-and-volleyball-at-torrance-beach-3/&quot;
              [&quot;menu_order&quot;]=&gt;
              int(0)
              [&quot;post_type&quot;]=&gt;
              string(7) &quot;listing&quot;
              [&quot;post_mime_type&quot;]=&gt;
              string(0) &quot;&quot;
              [&quot;comment_count&quot;]=&gt;
              string(1) &quot;0&quot;
              [&quot;filter&quot;]=&gt;
              string(3) &quot;raw&quot;
            }
          }
  </code></pre>
  <p>I solved my problem this way.</p>
  <pre><code>                $result = new WP_Query( $query_args );
              $sortResult = [];
              $keysArr = [];
              $eventStDate = &quot;&quot;;
              $diffDays = 0;
              $interval = 0;
              $returningSortResult = $result;
  
              foreach ($result-&gt;posts as $ppkey =&gt; $ppvalue) {
                  $eventStDate = get_post_meta($ppvalue-&gt;ID, &quot;_event_date&quot;, false);
                  $interval = date_diff(new DateTime(date( &quot;Y-m-d&quot;,   strtotime($nowDate) )), new DateTime(date( &quot;Y-m-d&quot;,   strtotime($eventStDate[0]) )));
                  $sortResult[][&quot;key&quot;] = array($interval-&gt;d, $ppvalue);
              }
              asort($sortResult);
              $theKey = 0;
              foreach ($sortResult as $sortKey =&gt; $sortValue) {
                  $keysArr[][&quot;key&quot;] = $sortKey;
                  $returningSortResult-&gt;posts[$theKey] = $sortValue[&quot;key&quot;][1];
                  $theKey++;
              }
  
              return  $returningSortResult;
  </code></pre>
  <p>but I'm sure there is a better solution. I would like to improve this sorting.</p>
  <p>sorry for my English :)</p>
  <p>Thanks.</p>