Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

php - How to sort an array by date?

I have an array with these dates.

.array(34) {
  [0]=>
  string(24) "Wed Nov 14 2012 09:27:49"
  [1]=>
  string(24) "Tue Nov 13 2012 15:49:06"
  [2]=>
  string(24) "Wed Nov 14 2012 09:27:49"
  [3]=>
  string(24) "Mon Dec 03 2012 13:58:16"
  [4]=>
  string(24) "Tue Nov 13 2012 14:20:16"
  [5]=>
  string(24) "Mon Dec 03 2012 16:13:38"
  [6]=>
  string(24) "Thu Aug 30 2012 15:05:56"
  [7]=>
  string(24) "Thu Aug 30 2012 15:11:32"
  [8]=>
  string(24) "Thu Aug 30 2012 15:21:12"
  [9]=>
  string(24) "Fri Oct 05 2012 10:11:28"
  [10]=>
  string(24) "Thu Aug 30 2012 15:22:58"
  [11]=>
  string(24) "Thu Aug 30 2012 14:38:13"
  [12]=>
  string(24) "Thu Aug 30 2012 15:22:00"
  [13]=>
  string(24) "Thu Aug 30 2012 15:03:15"
  [14]=>
  string(24) "Thu Aug 30 2012 15:00:11"
  [15]=>
  string(24) "Thu Aug 30 2012 15:01:25"
  [16]=>
  string(24) "Thu Aug 30 2012 15:20:26"
  [17]=>
  string(24) "Thu Aug 30 2012 14:59:21"
  [18]=>
  string(24) "Thu Aug 30 2012 15:26:48"
  [19]=>
  string(24) "Thu Aug 30 2012 14:58:34"
  [20]=>
  string(24) "Wed Sep 19 2012 09:42:57"
  [21]=>
  string(24) "Thu Aug 30 2012 15:24:34"
  [22]=>
  string(24) "Thu Aug 30 2012 15:06:50"
  [23]=>
  string(24) "Thu Aug 30 2012 14:57:42"
  [24]=>
  string(24) "Thu Aug 30 2012 15:09:06"
  [25]=>
  string(24) "Thu Aug 30 2012 14:53:13"
  [26]=>
  string(24) "Thu Aug 30 2012 15:25:35"
  [27]=>
  string(24) "Thu Oct 11 2012 15:19:10"
  [28]=>
  string(24) "Wed Oct 10 2012 11:27:15"
  [29]=>
  string(24) "Mon Dec 03 2012 16:24:06"
  [30]=>
  string(24) "Thu Nov 01 2012 10:00:42"
  [31]=>
  string(24) "Wed Nov 21 2012 11:08:36"
  [32]=>
  string(24) "Mon Dec 03 2012 16:14:29"
  [33]=>
  string(24) "Mon Dec 03 2012 16:30:31"
}

How do I sort this array by the date and the time? For example, "Mon Jan 01 2012 01:01:01" would be the first element in the array, and "Mon Jan 01 2012 01:01:03" would be the second element in the array.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a code, it is not the best solution, but it should be work

function sort_date($arr) {
  $size = count($arr);
  for ($i=0; $i<$size; $i++) {
      for ($j=0; $j<$size-1-$i; $j++) {
          if (strtotime($arr[$j+1]) < strtotime($arr[$j])) {
              swap($arr, $j, $j+1);
          }
      }
  }
  return $arr;
}

function swap(&$arr, $a, $b) {
  $tmp = $arr[$a];
  $arr[$a] = $arr[$b];
  $arr[$b] = $tmp;
}

//your first array
var_dump($arr);
//your sorted array
var_dump(sort_date($arr));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...