2013-07-22 70 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"; 
}