0

我正在尝试在每个月的 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 年 8 月 30 日/2013 年 9 月 13 日。我希望它在 2013 年 8 月 2 日之后发布,因为其余日期为时已晚。

4

1 回答 1

0

您在以下内容中有错字。它应该$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";
}
于 2013-07-22T18:31:53.433 回答