2011-12-28 47 views
0

的設定所以我花了一整天寫這個頁面拉存儲在表中特定組會議,日曆列表,改變個月

放棄任何會議,年齡超過4個月,並進一步在未來的比10個月,所以它顯示了一年的會議價值,但從3個月前開始,所以你可以看到最近發生的事情,如果你想除了會發生什麼。

一切正常 - 除了天+/-日期接縫非常字面 比較,我今天跑了測試27日和會議我在DB 28日和30日現身在一月

我可以將日期舍入到當前月份的第一個月份嗎?

有沒有更簡單的方法來寫這段代碼?

PHP代碼:

// post variables 
$ScoutID=$_POST['ScoutID']; 
$Rank=$_POST['Rank']; 
$DenID=$_POST['DenID']; 
// Define Dates 
$Date = date("Y-m-d"); 
$Month1 = date("Y-m-d", strtotime("-3 months")); 
$Month2 = date("Y-m-d", strtotime("-2 months")); 
$Month3 = date("Y-m-d", strtotime("-1 months")); 
$Month4 = $Date; 
$Month5 = date("Y-m-d", strtotime("+1 months")); 
$Month6 = date("Y-m-d", strtotime("+2 months")); 
$Month7 = date("Y-m-d", strtotime("+3 months")); 
$Month8 = date("Y-m-d", strtotime("+4 months")); 
$Month9 = date("Y-m-d", strtotime("+5 months")); 
$Month10 = date("Y-m-d", strtotime("+6 months")); 
$Month11 = date("Y-m-d", strtotime("+7 months")); 
$Month12 = date("Y-m-d", strtotime("+8 months")); 
$Month12 = date("Y-m-d", strtotime("+9 months")); 
//counter variables to print the month header 
$M1=0; 
$M2=0; 
$M3=0; 
$M4=0; 
$M5=0; 
$M6=0; 
$M7=0; 
$M8=0; 
$M9=0; 
$M10=0; 
$M12=0; 
$M12=0; 

//Find Den Number from Den ID 
$resultb = mysql_query('SELECT Den FROM Dens WHERE DenID = "'.$DenID.'"'); 
    if (!resultb) 
    { 
    die('Could not query:' .mysql_error()); 
    } 
$DenNum = mysql_result($resultb,0); 

$query = 'SELECT * FROM DenMeetings WHERE DenID = "'.$DenID.'" ORDER BY Date'; 
$result = mysql_query($query) or die(mysql_error()); 

while($row = mysql_fetch_array($result)) 
    { 

    if($row['Date'] > $Month1 && $row['Date'] < $Month2) 
     { 
     if($M1 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month2)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M1++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month2 && $row['Date'] < $Month3) 
     { 
     if($M2 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month3)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 2 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M2++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 2 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month3 && $row['Date'] < $Month4) 
     { 
     if($M3 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month4)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 3 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M3++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 3 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month4 && $row['Date'] < $Month5) 
     { 
     if($M4 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month5)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M4++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month5 && $row['Date'] < $Month6) 
     { 
     if($M5 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month6)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M5++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month6 && $row['Date'] < $Month7) 
     { 
     if($M6 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month7)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M6++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month7 && $row['Date'] < $Month8) 
     { 
     if($M7 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month8)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M7++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month8 && $row['Date'] < $Month9) 
     { 
     if($M8 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month9)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M8++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month9 && $row['Date'] < $Month10) 
     { 
     if($M9 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month10)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M9++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month10 && $row['Date'] < $Month11) 
     { 
     if($M10 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month11)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M10++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month11 && $row['Date'] < $Month12) 
     { 
     if($M11 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month12)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M11++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else if($row['Date'] > $Month12 && $row['Date'] < $Month13) 
     { 
     if($M12 < 1) 
      { 
      echo '<h2>'; 
      echo strftime('%B',strtotime($Month11)); 
      echo '</h2>'; 

      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      $M12++; 
      } 
     else 
      { 
      echo $row['Date']; 
      echo ' - 1 - '; 
      echo $row['Notes']; 
      echo '<br>'; 
      } 
     } 
    else 
     { 

     } 
    } 

輸出 - 注意的具體日期,1月:

2011-11-26 - 2 - 1個月

2011-1 2-15 - 3 -

2011-12-20 - 3 - test32

2011-12-24 - 3 - 測試

2011-12-26 - 3 - 今天

一月

** 2011-12-28 - 1 - 測試

2011-12-30 - 1 - 試驗**

2012-01-05 - 1 - test23

2012-01-13 - 1 - 12

2012-01-13 - 1 - 12

2012-01-13 - 1 - 12

2012-01 -20 - 1 - 32w45

2012-04-26 - 1 - 4個月

2012-04-26 - 1 - +4個月

+2

必須有* *了以一種更好的方式來做到這一點 - 我敢打賭,它與一個'for'循環開始。 – Ben 2011-12-28 02:09:26

+0

它與[數組](http://www.php.net/manual/en/language.types.array.php)以及 – 2011-12-28 02:20:48

+0

我得到四捨五入的部分..... – PHPFouts 2011-12-28 02:31:41

回答

0

這是聖誕節,所以在這裏你去。它做你所要求的一切(我認爲)並且增加了一層靈活性。

幾個比特可以優化,但它工作得很好。

$data = array(); 

// Generate some random dates to simulate database output, then sort. 
for($x=0;$x<100;$x++) { 
    $day = sprintf("%02s", rand(1,29)); 
    $month = sprintf("%02s", rand(1,12)); 
    $year = rand(2011, 2013); 
    $data[] = array("Date"=>$year."-".$month."-".$day, "Notes"=>"Entry ".$x); 
} 

sort($data); 

// Calculate start/end timestamps (YYYYMM) according to 
// start/end index prefs and current year 
$startIndex = -3; 
$endIndex = 9; 
$startTimestamp = date("Ym", mktime(0, 0, 0, date('n') + $startIndex, 1, 2012)); 
$endTimestamp = date("Ym", mktime(0, 0, 0, date('n') + $endIndex, 1, 2012)); 

// Loop through data 
$previous = ""; 
for($y=0;$y<count($data);$y++) { 
    // Create timestamp for comparison (YYYYMM) 
    // by removing delimiter in "date" value. 
    $entry = $data[$y]; 
    $date = explode("-", $entry["Date"]); 
    $timestamp = $date[0].$date[1]; 

    // If the timestamp is outside of the start/end timestamps, 
    // tell loop to move on to next value 
    if($timestamp < $startTimestamp || $timestamp > $endTimestamp) { 
    continue; 
    } 

    // If the timestamp has changed month, 
    // output the title of the new month 
    if($timestamp != $previous) { 
    echo "<br /><b>"; 
    echo date("F Y", mktime(0, 0, 0, $date[1], 1, $date[0])); 
    echo "</b><br />"; 
    } 

    // Finally, output individual values 
    echo $entry["Date"].": ".$entry["Notes"]."<br />"; 

    // Save current timestamp for comparison on the next round 
    $previous = $timestamp; 
} 

編輯的第一部分,只是從數據庫模擬數據,您可以根據您的需要,當然削減了這一點。它的核心是中間的for循環。

輸出:

enter image description here

+0

我非常喜歡這種與我相比的簡單,我顯然在PHP中學到了很多東西。但是你生成了一個數組,其中的每個部分都是Year-Month-Date-Notes,並且你分解了這些數據以便在屏幕上打印它們,但是我的查詢不會以這種格式放置信息,我如何得到$ query ='SELECT *從DenMeetings WHERE DenID =「'。$ DenID。'」ORDER BY Date'; $ result = mysql_query($ query)或die(mysql_error()); while($ row = mysql_fetch_array($ result))爲了適合你的腳本? – PHPFouts 2011-12-29 02:41:12