-1

我有一个像这样的日期数组

$dateArray =   array('2012-04-11','2012-04-29','2012-04-26','2012-04-23','2012-03-21','2012-07-23','2012-12-19','2012-04-27','2012-05-12','2012-05-27','2012-05-28');

我使用代码过滤这个数组是

但它不工作

$start_date= date('Y-m-d',strtotime('first day of this month')) ;
$end_date =date('Y-m-d',strtotime('first day of next month')) ;

$month = array();

foreach ($dateArray as $dates)
{
 $dateTimes = strtotime($dates);

 if (($start_date >= $dateTimes) && ($end_date <= $dateTimes))
 {
   $month[]= $dates;
 } 
}
var_dump($month);

但它不工作

4

2 回答 2

1

您需要比较 unix 时间戳才能正确进行比较:

$dateArray  = array('2012-04-11','2012-04-29','2012-04-26','2012-04-23','2012-03-21','2012-07-23','2012-12-19','2012-04-27','2012-05-12','2012-05-27','2012-05-28');
$start_date = strtotime('first day of this month') ;
$end_date   = strtotime('first day of next month') ;

$month = array();

foreach ($dateArray as $dates)
{
    $dateTimes = strtotime($dates);

    if (($start_date >= $dateTimes) && ($end_date <= $dateTimes))
    {
        $month[]= $dates;
    } 
}
var_dump($month);
于 2012-04-27T11:28:00.863 回答
0

嗨,如果您只想显示具有当前月份的数组中的日期,那么

  foreach($dateArray AS $dateArr){

        if(date('M') == date('M' , strtotime($dateArr))){
            echo $mnthDate[] = $dateArr;echo "<br/>";
        }
    }

否则,如果还想检查年份和月份是否与当前年份和月份相同。

 foreach($dateArray AS $dateArr){
        if((date('M') == date('M' , strtotime($dateArr))) && (date('Y') == date('Y' , strtotime($dateArr)))){
            $yrDate[] = $dateArr;echo "<br/>";
        }
    }
于 2012-04-27T11:37:07.417 回答