比较foreach php数组后的日期


Comparing dates after a foreach php array

我试图为每个月的8号添加一条语句,我已经多次比较了日期,但无法将其列在正确的日期旁边。

<?php
$month_arr = Array( 
            'July' => Array('num_dates'=>0, 'dates'=>Array()), 
            'August' => Array('num_dates'=>0, 'dates'=>Array()), 
            'September' => Array('num_dates'=>0, 'dates'=>Array()), 
            'October' => Array('num_dates'=>0, 'dates'=>Array()), 
            'November' => Array('num_dates'=>0, 'dates'=>Array()), 
            'December' => Array('num_dates'=>0, 'dates'=>Array()),
            'January' => Array('num_dates'=>0, 'dates'=>Array()) , 
            'February' => Array('num_dates'=>0, 'dates'=>Array()), 
            'March' => Array('num_dates'=>0, 'dates'=>Array()), 
            'April' => Array('num_dates'=>0, 'dates'=>Array()), 
            'May' => Array('num_dates'=>0, 'dates'=>Array()), 
            'June' => Array('num_dates'=>0, 'dates'=>Array())
        );
 $date_arr = Array();
 $date_start = '07/19/2013';
 $date_arr[] = date('M j, Y', strtotime($date_start));

for ($i=1; $i<=4; $i++){
    $date_temp = date('M j, Y', strtotime($date_arr[$i-1] . " + 14 day"));
    $month = date('F', strtotime($date_temp));
    $month_arr[$month]['dates'][] = $date_temp;
    $month_arr[$month]['num_dates'] += 1;
    $date_arr[] = $date_temp;
}
foreach ($month_arr as $k => $v){
    if (!empty($v)){
        if ($v['num_dates'] != 0){
            echo "<BR><BR>Month: " . $k;
            echo "<BR>No. of dates: " . $v['num_dates'];
            foreach ($v['dates'] as $k1=>$v1){
                 echo "<BR>" .$v1;
              $event = 'Aug 8, 2013';
            if($event>$v && !($event<$v1)) {
            echo "Event belongs here on $v1";
              }
            else {
            echo "Event does not belongs here it's to late on the on $v1";
              }
            }
        }
     }  
}
?>

印刷版将于2013年8月2日/2013年8月16日/2013月30日/2013 9月13日。我希望它在2013年8月2日之前进行,因为其他日期都太晚了。

您在下面有一个拼写错误。它应该是$v1而不是$v:

$event = 'Aug 8, 2013';
if($event>$v && !($event<$v1)) {
    echo "Event belongs here on $v1";
}else {
    echo "Event does not belongs here it's to late on the on $v1";
}

此外,您正在比较字符串值。你需要将它们转换为你可以比较的东西,比如时间戳:

$event = strtotime('Aug 8, 2013');
$vts = strtotime($v1);
if($event > $vts) {
    echo "Event belongs here on $v1";
}else {
    echo "Event does not belongs here it's to late on the on $v1";
}