【发布时间】: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