4

I am trying to sort an array in PHP by date and time which is in ISO 8601 format. I am still trying to grasp PHP and have tried many of the solutions on stack overflow and I am just not able to nail down the right function. Hopefully this is an easy answer and it will be helpful to others.

FYI, this array was generated by the Citrix API for GoToMeeting. I would like to sort the array based on startTime in the soonest time first in the list.

Here is what the array looks like using var_export with two results presented:

array (
 0 => stdClass::__set_state(
  array(
   'createTime' => '2012-07-03T19:36:58.+0000',
   'status' => 'INACTIVE',
   'subject' => 'Client 1',
   'startTime' => '2012-07-10T14:00:00.+0000',
   'conferenceCallInfo' => 'United States: xxxxx Access Code: xxxxx',
   'passwordRequired' => 'false',
   'meetingType' => 'Scheduled',
   'maxParticipants' => 26,
   'endTime' => '2012-07-10T15:00:00.+0000',
   'uniqueMeetingId' => 12345678,
   'meetingid' => 123456789,
  )
 ),
 1 => stdClass::__set_state(
  array(
   'createTime' => '2012-07-02T21:57:48.+0000',
   'status' => 'INACTIVE',
   'subject' => 'Client 2',
   'startTime' => '2012-07-06T19:00:00.+0000',
   'conferenceCallInfo' => 'United States: xxxxx Access Code: xxxxx',
   'passwordRequired' => 'false',
   'meetingType' => 'Scheduled',
   'maxParticipants' => 26,
   'endTime' => '2012-07-06T20:00:00.+0000',
   'uniqueMeetingId' => 12345678,
   'meetingid' => 123456789,
  )
 ),
)

My goal is to then output the array into html div's using a foreach loop, this code is complete and works well but my sort is off :-)

Thank you in advance for any help!

Steve

ghoti
  • 45,319
  • 8
  • 65
  • 104
Steven Carlton
  • 422
  • 4
  • 23
  • 1
    The nice thing about iso8601 is you can generally just do a simple string sort and get them in the right order. Look into [usort()](http://php.net/usort) – Marc B Jul 04 '12 at 03:39

1 Answers1

4

You can implement any sorting technique you can think of if you wrap it in a callback and use usort() docs here

inside your callback, you can use strtotime or similar, and do simple int comparisons.

$myDateSort = function($obj1, $obj2) {
  $date1 = strtotime($obj1->startTime);
  $date2 = strtotime($obj2->startTime);
  return $date1 - $date2; // if date1 is earlier, this will be negative
}
usort($myArray, $myDateSort);
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • Thanks for the code snippet, unfortunately this is not affecting the sort of my results. Trying to understand the code, is the usort generating the $obj1, $obj2 inputs for the function? – Steven Carlton Jul 04 '12 at 05:54
  • Kinda... When you call usort and pass it an array, it calls your function a number of times, each time passing in 2 of the objects in your array. So $obj1 and $obj2 are each instances of whatever your array is keeping as it's values (I think your code indicates they are stdClass instances). Inside the function, you just evaluate if the first is "less than" the second, and if so return a negative int, else positive (or zero for same). PHP takes care of actually sorting the array based on calling your function for most pairs of array values. – Chris Trahey Jul 04 '12 at 06:16
  • After taking a second look I was able to get my array to sort and output in the correct order. A note to others if you use the code provided in the answer and the results are not sorting, try using usort($this->$myArray, $myDateSort); – Steven Carlton Jul 04 '12 at 13:46