想要当前所有月份的PHP日期

时间:2012-04-27 11:22:45

标签: php mysql

我有像这样的日期数组

$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);

但它不起作用

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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/>";
        }
    }