按 ISO 日期对 PHP 数组进行排序


Sorting PHP Array by ISO Date

我正在尝试按日期和时间对 PHP 中的数组进行排序,该数组采用 ISO 8601 格式。我仍在尝试掌握PHP,并且已经尝试了许多堆栈溢出的解决方案,但我只是无法确定正确的功能。希望这是一个简单的答案,对其他人有帮助。

仅供参考,此数组由用于GoToMeeting的Citrix API生成。我想在列表中最快的时间根据 startTime 对数组进行排序。

以下是使用var_export数组的外观,并提供了两个结果:

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,
  )
 ),
)

我的目标是然后使用 foreach 循环将数组输出到 htmldiv 中,这段代码是完整的并且运行良好,但我的排序是关闭的:-)

提前感谢您的任何帮助!

史蒂夫

如果你

把它包装在回调中并在这里使用usort()文档,你可以实现你能想到的任何排序技术

在你的回调中,你可以使用strtotime或类似的,并进行简单的int比较。

$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);