如何从具有相同年份,月份和日期的数组中删除更长时间的日期?

时间:2012-10-10 07:21:20

标签: php date

在PHP中如何从具有相同年份,月份和日期的数组中删除日期更长的时间?

array(7) { 
    [0]=> string(19) "2012-06-11 08:30:49" 
    [1]=> string(19) "2012-06-11 08:03:54" 
    [2]=> string(19) "2012-05-26 23:04:04" 
    [3]=> string(19) "2012-05-27 08:30:00" 
    [4]=> string(19) "2012-06-08 08:30:55" 
    [5]=> string(19) "2012-06-08 08:31:12" 
    [6]=> string(19) "2012-06-08 08:55:24"
}

将删除array[0],array[5],array[6]

输出:

('2012-06-11 08:03:54',''2012-05-26 23:04:04','2012-05-27 08:30:00','2012-06-08 08:30:55')

OR

'2012-06-11 08:03:54'
'2012-05-26 23:04:04'
'2012-05-27 08:30:00'
'2012-06-08 08:30:55'

换句话说,我只希望在不同的日子里有最早的约会。

4 个答案:

答案 0 :(得分:1)

$filtered = array();
foreach ($array as $time) {
    $day = date('Y-m-d', strtotime($time));
    if (!isset($filtered[$day]) || strtotime($filtered[$day]) > strtotime($time)) {
        $filtered[$day] = $time;
    }
}

答案 1 :(得分:1)

$array = array( 
    "2012-06-11 08:30:49" ,
    "2012-06-11 08:03:54" ,
    "2012-05-26 23:04:04" ,
    "2012-05-27 08:30:00" ,
    "2012-06-08 08:30:55" ,
    "2012-06-08 08:31:12" ,
    "2012-06-08 08:55:24"
);

$new_array = array();
foreach($array as $item){
    $item_array = explode(" ", $item);
    $date = $item_array[0];
    $time = $item_array[1];
    if(!isset($new_array[$date]) || $time < $new_array[$date])
        $new_array[$date] = $time;
}

$final = array();

foreach($new_array as $key => $value){
    $final[] = $key . ' ' . $value;
}

print_r($final);

答案 2 :(得分:1)

您可以尝试使用usort

$array = array( 
    "2012-06-11 08:30:49" ,
    "2012-06-11 08:03:54" ,
    "2012-05-26 23:04:04" ,
    "2012-05-27 08:30:00" ,
    "2012-06-08 08:30:55" ,
    "2012-06-08 08:31:12" ,
    "2012-06-08 08:55:24"
);


usort($array, function ($a, $b) {
    $a = strtotime($a);
    $b = strtotime($b);
    return ($a == $b) ? 0 : (($a > $b) ? - 1 : 1);
});

$list = array();
foreach ( $array as $time ) {
    $day = date('Y-m-d', strtotime($time));
    if (! array_key_exists($day, $list))
        $list[$day] = $time;
}

var_dump($list);

输出

array
  '2012-06-11' => string '2012-06-11 08:30:49' (length=19)
  '2012-06-08' => string '2012-06-08 08:55:24' (length=19)
  '2012-05-27' => string '2012-05-27 08:30:00' (length=19)
  '2012-05-26' => string '2012-05-26 23:04:04' (length=19)

答案 3 :(得分:1)

一种方法是循环排序数组,在​​日期保持不变时消除更高的时间值。以下是工作代码:

echo '<pre>';
$in = array(
    '2012-06-11 08:30:49',
    '2012-06-11 08:03:54',
    '2012-05-26 23:04:04',
    '2012-05-27 08:30:00',
    '2012-06-08 08:31:12',
    '2012-06-08 08:30:55',
    '2012-06-08 08:55:24',
);
var_dump($in);

$out = $in;
sort($out, SORT_STRING);
for($i=count($out)-2; $i>=0; --$i)
{
    list($d1, $t1) = explode(' ', $out[$i+1]);
    list($d2, $t2) = explode(' ', $out[$i]);
    if($d1 == $d2 && $t2 <= $t1) //same date and lower time
    {
        unset($out[$i+1]);
    }
}

var_dump($out);
echo '</pre>';

如果元素的顺序不重要,这种方法将足够快,因为它使用php的sort()函数,并且在代码中完成的唯一耗时的事情是循环遍历数组,这需要O( n)时间。

此外,如果您不需要保留原始数组,则可以将$out替换为$in,并删除$out=$in行。这将提高内存使用率,因为不会创建额外的数组。