-4

如何从某个月份的数组中查找日期?数组结构为:

Array ( [0] => 2013-05-23 
        [1] => 2013-05-24 
        [2] => 2013-05-25 
        [3] => 2013-05-26 
        [4] => 2013-05-27 
        [5] => 2013-06-02 
        [6] => 2013-06-03 
        [7] => 2013-06-04 )

我需要给数组提供日期、月份数的函数,它返回包含该月日期的数组。

4

2 回答 2

2

我会使用 date_parse 内置函数,它返回日期数组

$dates = array(
    0 => '2013-05-23', 
    1 => '2013-05-24',
    2 => '2013-05-25',
    3 => '2013-05-26', 
    4 => '2013-05-27', 
    5 => '2013-06-02', 
    6 => '2013-06-03', 
    7 => '2013-06-04'

);

$date = getDate(05, $dates);

function getDate($month, $dates){
    $return = array();
    foreach($dates as $date){
    $check = date_parse($date);
        if($check['month'] == $month){
            array_push($return, $date);
        }
    }
return $return;
}
于 2013-05-06T21:18:47.263 回答
1
function narrowByMonth($dates, $monthNumber) {
    foreach ($dates as $date) {
        $split = explode('-', $date);
        $year = $split[0]; // Not needed in this example
        $month = $split[1];
        $day = $split[2]; // Not needed in this example
        if ($month == $monthNumber) {
            echo $date.'<br />';
        }
    }
}

$dates = array ('2013-05-25',
    '2013-05-26',
    '2013-06-02',
    '2013-06-03');

$monthNumber = '05';

narrowByMonth($dates, $monthNumber);

将输出:

2013-05-25
2013-05-26

于 2013-05-06T20:51:07.057 回答