2013-07-22 29 views
0

我试图为每个月的第8个添加一个语句,我已经比较了日期多种方式,但无法将其置于恰当日期旁边的状态。比较foreach php数组后的日期

<?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日/二零一三年八月三十日/ 9月13日,2013年我希望它旁边去2013年8月2日的日期,其余都来不及。

+0

你在这个声明中'$ date_temp =日期( 'MĴ,Y' 的strtotime做($ date_arr [$ I-1 ]; 14));' – DevZer0

+0

@ DevZer0从OP看这个问题:http://stackoverflow.com/questions/17731572/grouping-dates-into-months-in-php。这可能会给你一些想法。我在上面的代码中看到一些语法错误。你在这里得到任何具体的错误? – Maximus2012

+0

修复了语法错误 – Zowes

回答

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"; 
}