我正在创建一个小预订系统。行设置为当前周列到特定时间。
我要做的是将日期名称更改为特定颜色。过去的蓝色,日期当前为绿色。不知怎的,if
语句没有做任何事情。任何人都可以解释或指出我正确的方向吗?
$today = time();
$lastWeek = [];
$lastWeek[0] = date('D',strtotime('last monday'));
$lastWeek[1] = date('D',strtotime('last monday +1day'));
$lastWeek[2] = date('D',strtotime('last monday +2day'));
$lastWeek[3] = date('D',strtotime('last monday +3day'));
$lastWeek[4] = date('D',strtotime('last monday +4day'));
$timeSlot = [];
$timeSlot [0] = date('H:i', strtotime('9:00')) ."-9:30";
$timeSlot [1] = date('H:i', strtotime('9:30')) ."-10:00";
$timeSlot [2] = date('H:i', strtotime('10:00')) ."-10:30";
$timeSlot [3] = date('H:i', strtotime('10:30')) ."-11:00";
$timeSlot [4] = date('H:i', strtotime('11:00')) ."-11:30";
$timeSlot [5] = date('H:i', strtotime('11:30')) ."-12:00";
echo "<html><head><title>Doctor Booking timetable</title></head>";
echo "<body><table id=myTable border=2 align=center >";
echo "<tr><td>Week Commencing</td>";
foreach ($timeSlot as $time)
{
echo"<td>";
echo $time;
echo"</td>";
}
echo "</tr>";
foreach ($lastWeek as $day)
{
echo "<tr><td>";
echo $day;
echo "</td></tr>";
}
if ($lastWeek < $today){
echo "<font color='blue'></font>";
}else {
echo "<font color='green'></font>";
}
答案 0 :(得分:2)
您需要将if
语句放在foreach
内(如您的帖子标题所示),并在您的颜色标记中包含$day
。仅供参考font
代码已弃用很长时间,您应该使用span
代替。
在您的问题中,您要将$lastWeek
与$today
进行比较 - $lastWeek
是一个数组,您正在循环播放。根据您的foreach
,当前迭代将包含在$day
变量中,因此您应将 与$today
进行比较:
foreach ($lastWeek as $day)
{
echo "<tr><td>";
$color = ($day < $today) ? 'blue' : 'green';
echo '<span style="color: ' . $color . '">' . $day . '</span>';
echo "</td></tr>";
}
PS:我在这里使用ternary operator作为你的if语句。这和写作一样:
if($day < $today)
$color = 'blue';
else
$color = 'green';
......但更短
PPS。我假设您的日期格式正确,可以使用GT / LT运算符进行比较。如果不是,则需要使用date function之类的格式对其进行格式化。
答案 1 :(得分:0)
除非非常需要,否则我不会使用已弃用的标签。 请参阅http://www.w3schools.com/tags/tag_font.asp
中的相关内容使用您的代码样式:
$today = time();
$lastWeek = array();
$lastWeek[0] = date('D',strtotime('last monday'));
$lastWeek[1] = date('D',strtotime('last monday +1day'));
$lastWeek[2] = date('D',strtotime('last monday +2day'));
$lastWeek[3] = date('D',strtotime('last monday +3day'));
$lastWeek[4] = date('D',strtotime('last monday +4day'));
$timeSlot = array();
$timeSlot [0] = date('H:i', strtotime('9:00')) ."-9:30";
$timeSlot [1] = date('H:i', strtotime('9:30')) ."-10:00";
$timeSlot [2] = date('H:i', strtotime('10:00')) ."-10:30";
$timeSlot [3] = date('H:i', strtotime('10:30')) ."-11:00";
$timeSlot [4] = date('H:i', strtotime('11:00')) ."-11:30";
$timeSlot [5] = date('H:i', strtotime('11:30')) ."-12:00";
echo "<html><head><title>Doctor Booking timetable</title></head>";
echo "<body><table id=myTable border=2 align=center >";
echo "<tr><td>Week Commencing</td>";
foreach ($timeSlot as $time)
{
echo"<td>";
echo $time;
echo"</td>";
}
echo "</tr>";
foreach ($lastWeek as $day)
{
echo "<tr><td>";
if ($lastWeek < $today){
echo "<font color='blue'>$day</font>";
}else {
echo "<font color='green'>$day</font>";
}
echo "</td></tr>";
}