日历列表,用于更改月份集

时间:2011-12-28 02:07:02

标签: php calendar while-loop date strtotime

所以我整天都在写这个页面来拉出存储在特定组的表中的会议,

丢弃任何超过4个月的会议,以及未来10个月的会议,所以它显示了一年的会议但从3个月前开始,所以你可以看到最近发生了什么,如果你想要除了什么是将要发生。

一切正常 - 除了天+/-日期接缝的比较非常直观 我今天在27日进行了一次测试,我在2月28日和30日在数据库中的会议将在1月份开始。

我可以将日期舍入到当月的第一天吗?

是否有更简单的方法来编写此代码?

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-12-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个月

1 个答案:

答案 0 :(得分: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