0

$dates = array(下面的日期);

我将如何按月、日、年对这个数组进行排序,然后按如下所示的时间戳排序?

Array (
0 => 'Thu Aug 30 2012 14:38:13 GMT-0700 (PDT)',
1 => 'Thu Aug 30 2012 14:53:13 GMT-0700 (PDT)',
2 => 'Thu Aug 30 2012 14:57:42 GMT-0700 (PDT)',
3 => 'Thu Aug 30 2012 14:58:34 GMT-0700 (PDT)',
4 => 'Thu Aug 30 2012 14:59:21 GMT-0700 (PDT)',
5 => 'Thu Aug 30 2012 15:00:11 GMT-0700 (PDT)',
6 => 'Thu Aug 30 2012 15:01:25 GMT-0700 (PDT)',
7 => 'Thu Aug 30 2012 15:03:15 GMT-0700 (PDT)',
8 => 'Thu Aug 30 2012 15:05:56 GMT-0700 (PDT)',
9 => 'Thu Aug 30 2012 15:06:50 GMT-0700 (PDT)',
10 => 'Thu Aug 30 2012 15:09:06 GMT-0700 (PDT)',
11 => 'Thu Aug 30 2012 15:11:32 GMT-0700 (PDT)',
12 => 'Thu Aug 30 2012 15:20:26 GMT-0700 (PDT)',
13 => 'Thu Aug 30 2012 15:21:12 GMT-0700 (PDT)',
14 => 'Thu Aug 30 2012 15:22:00 GMT-0700 (PDT)',
15 => 'Thu Aug 30 2012 15:22:58 GMT-0700 (PDT)',
16 => 'Thu Aug 30 2012 15:24:34 GMT-0700 (PDT)',
17 => 'Thu Aug 30 2012 15:25:35 GMT-0700 (PDT)',
18 => 'Thu Aug 30 2012 15:26:48 GMT-0700 (PDT)',
19 => 'Wed Sep 19 2012 09:42:57 GMT-0700 (PDT)',
20 => 'Fri Oct 05 2012 10:11:28 GMT-0700 (PDT)',
21 => 'Wed Oct 10 2012 11:27:15 GMT-0700 (PDT)',
); 
4

2 回答 2

1

strtotime() 将每个时间转换为 unix 时间戳,然后 date('Ymd H:i:s') 按日期排序

例如:

$unix =  strtotime('Thu Aug 30 2012 14:38:13 GMT-0700 (PDT)');

echo date('Y-m-d H:i:s', $unix);

将输出:

2012-10-11 15:25:42

你可以排序

于 2012-10-11T19:24:26.250 回答
1

可能有更好的解决方案,但这应该有效:

foreach ($dates as $value) {
    $newDates[strtotime($value)] = $value;
}

$newDates = ksort($newDates);

这将使新数组$newDates具有时间戳的键并按键排序。这将以您已有的格式维护数组的值。如果您想更改格式,可以执行以下操作:

$newDates[strtotime($value)) = date("Y-m-d H:i:s", strtotime($value)); // 2012-11-10 12:27:00
于 2012-10-11T19:25:14.660 回答