【问题标题】:Calendar list, for changing set of months日历列表,用于更改月份
【发布时间】:2011-12-28 02:07:02
【问题描述】:

所以我花了一整天的时间写这个页面来为特定组提取存储在表格中的会议,

放弃任何超过 4 个月和未来超过 10 个月的会议,因此它会显示一年的会议,但从 3 个月前开始,这样您就可以看到最近发生的事情,除了会发生的。

一切正常 - 除了天数的比较 +/- 日期接缝非常真实 我今天在 27 号进行了测试,我在 28 号和 30 号在 DB 举行的会议出现在 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
        {

        }
    }

输出 - 注意一月的日期:

十一月

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

【问题讨论】:

  • got 是一种更好的方法来做到这一点 - 我敢打赌它从 for 循环开始。
  • 也和arrays有关
  • 我得到了四舍五入的月份部分.....
  • $Month12 = date("Y-m-d",strtotime("-1 秒",strtotime("+10 月",strtotime(date("m")."/01/".date( "Y")." 00:00:00"))));

标签: php calendar while-loop date strtotime


【解决方案1】:

今天是圣诞节,来吧。它可以满足您的所有要求(我认为),还增加了一层灵活性。

可以优化一些位,但效果很好。

$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循环。

输出:

【讨论】:

  • 我喜欢这与我的比较简单,而且我显然在 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)) 适合你的脚本吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 2015-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多