結果がどのように得られたのかはわかりませんが、間違っているように見えます.4つの異なる結果になる可能性があるためです。
想像
$data = array(
0 => Array ( 'id' => 47523, 'date' => "12-02-13 14:36:32" ),
1 => Array ( 'id' => 47523, 'date' => "12-02-13 13:56:48" ),
2 => Array ( 'id' => 65454, 'date' => "12-02-13 14:36:32" ),
3 => Array ( 'id' => 38639, 'date' => "12-02-13 13:56:48" ),
4 => Array ( 'id' => 38639, 'date' => "12-02-13 13:08:51" ),
5 => Array ( 'id' => 38639, 'date' => "12-02-13 13:07:43" ),
6 => Array ( 'id' => 38639, 'date' => "12-02-13 13:09:43" ),
);
例
var_dump(getDiff($data));
var_dump(getDiff($data,SORT_ASC));
var_dump(getDiff($data,SORT_DESC));
SORT_NONE
array (size=3)
0 =>
array (size=2)
'id' => int 47523
'date' => string '12-02-13 13:56:48' (length=17)
1 =>
array (size=2)
'id' => int 38639
'date' => string '12-02-13 13:08:51' (length=17)
2 =>
array (size=2)
'id' => int 38639
'date' => string '12-02-13 13:07:43' (length=17)
出力 XSORT_ASC
array (size=4)
0 =>
array (size=2)
'id' => int 38639
'date' => string '12-02-13 13:08:51' (length=17)
1 =>
array (size=2)
'id' => int 38639
'date' => string '12-02-13 13:09:43' (length=17)
2 =>
array (size=2)
'id' => int 47523
'date' => string '12-02-13 13:56:48' (length=17)
3 =>
array (size=2)
'id' => int 47523
'date' => string '12-02-13 14:36:32' (length=17)
出力 XSORT_DESC
array (size=4)
0 =>
array (size=2)
'id' => int 65454
'date' => string '12-02-13 14:36:32' (length=17)
1 =>
array (size=2)
'id' => int 47523
'date' => string '12-02-13 13:56:48' (length=17)
2 =>
array (size=2)
'id' => int 38639
'date' => string '12-02-13 13:08:51' (length=17)
3 =>
array (size=2)
'id' => int 38639
'date' => string '12-02-13 13:07:43' (length=17)
使用する機能
function getDiff(array $data, $sort = 0) {
switch ($sort) {
case SORT_ASC :
usort($data, function ($a, $b) {
$a = strtotime($a['date']);
$b = strtotime($b['date']);
return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
});
break;
case SORT_DESC :
usort($data, function ($a, $b) {
$a = strtotime($a['date']);
$b = strtotime($b['date']);
return ($a == $b) ? 0 : (($a < $b) ? 1 : -1);
});
break;
}
$it = new CachingIterator(new ArrayIterator($data), CachingIterator::FULL_CACHE);
$list = array();
foreach ( $it as $var ) {
if ($it->hasNext()) {
$date = strtotime($var['date']);
$date2 = $it->getInnerIterator()->current();
$date2 = strtotime($date2['date']);
$diff = ($sort === SORT_DESC || $sort === 0) ? $date - $date2 : $date2 - $date ;
if ($diff <= (60 * 3)) {
$list[] = $it->getInnerIterator()->current();
}
}
}
return $list;
}
結論
要素内の日付の位置に依存します....