突出显示日历中的当前日期

I have a PHP code that generates a calendar. I'm trying to highlight the current date in this following code:

<?php
require("aacfs.php");

define("ADAY", (60*60*24));

if ((!isset($_POST['month'])) || (!isset($_POST['year']))) {
    $nowArray = getdate();
    $month = $nowArray['mon'];
    $year = $nowArray['year'];
} else {
    $month = $_POST['month'];
    $year = $_POST['year'];
}
$start = mktime(12,0,0,$month,1,$year);
$firstDayArray = getdate($start);
?>
<html>
<head>
<title><?php echo "Calendar: ".$firstDayArray['month']." " . $firstDayArray['year']; ?></title>
</head>
<script type="text/javascript">
function eventWindow(url) {
    var width = 500;
    var height = 500;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    event_popupWin = window.open(url, "subWind", windowFeatures, "POS", 'event', 'resizable=yes,scrollbars=yes,toolbar=no,width=400,height=400');
    event_popupWin.opener = self;
}
</script>
<script type="text/javascript">

    function hiliteToday(){
        var nDate = new Date();
        nDate = nDate.getDate();
        var nTable = document.getElementById('currMonth');
        nRows = nTable.rows.length;
        nCells = nTable.rows[0].cells.length;
        for (i=0; i<nRows; i++)
            {
             for (n=0; n<nCells; n++)
                {
                 var tmp = nTable.rows[i].cells[n].innerHTML;
                 tmp = tmp.split("<");
                 if (tmp[0] == nDate){nTable.rows[i].cells[n].style.backgroundColor = 'cyan'}
                }

            }
    }

    window.onload=hiliteToday;

</script>
<body>
<h1><center>Select a Month/Year</center></h1>
<center><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="month">
<?php
$months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

for ($x=1; $x<=count($months); $x++){
    echo "<option value=\"$x\"";
    if ($x == $month){
        echo " selected";
    }
    echo ">".$months[$x-1]."</option>";
}
?>
</select>
<select name="year">
<?php
for ($x=2012; $x<=2027; $x++){
    echo "<option";
    if ($x == $year){
        echo " selected";
    }
    echo ">$x</option>";
}
?>
</select>
<input type="submit" name="submit" value="Go">
</form></center>
<br />
<?php
$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "<center><table border=\"1\" cellpadding=\"5\" id=\"currMonth\"><tr>
";
foreach ($days as $day) {
    echo "<td style=\"background-color: #CCCCCC; text-align: center; width: 14%\">
          <strong>$day</strong></td>
";
}

for ($count=0; $count < (6*7); $count++) {
    $dayArray = getdate($start);
    if (($count % 7) == 0) {
        if ($dayArray["mon"] != $month) {
            break;
        } else {
            echo "</tr><tr>
";
        }
    }
    if ($count < $firstDayArray["wday"] || $dayArray["mon"] != $month) {
        echo "<td>&nbsp;</td>
";
    } else {
        $chkEvent_sql = "SELECT acode FROM reservation WHERE month(etd) = '".$month."' AND dayofmonth(etd) = '".$dayArray["mday"]."' AND year(etd) = '".$year."' ORDER BY etd";
        $chkEvent_res = mysql_query($chkEvent_sql) or die(mysql_error());

        if (mysql_num_rows($chkEvent_res) > 0) {
            $event_title = "<br/>";
            while ($ev = mysql_fetch_array($chkEvent_res)) {
                $event_title .= stripslashes($ev["acode"])."<br/>";
            }
            mysql_free_result($chkEvent_res);
        } else {
            $event_title = "";
        }

        echo "<td valign=\"top\"><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a><br/>".$event_title."</td>
";

        unset($event_title);

        $start += ADAY;
    }
}
echo "</tr></table></center>";
mysql_close();
?>
</br><center><a href="javascript:self.close()"><font face="consolas"><b>Close</b></font></a></center>
</body>
</html>

but it doesn't seem to work. The javasript function hiliteToday() is supposed to do work since I'm binding it in the <table id='currMonth'> but I don't get any result. What am I doing wrong? Please help me. Thanks in advance!

EDIT: Here's how the calendar looks like:

enter image description here

1个回答

You are checking with if (tmp[0] == nDate), if the table cell has the current day. In example "21" for today. But you never set only days to your table cells.

Change your 2nd for-loop to:

         for (n=0; n<nCells; n++)
            {
             var tmp = nTable.rows[i].cells[n].innerHTML;
             if (tmp.search(nDate)!=-1){nTable.rows[i].cells[n].style.backgroundColor = 'cyan'}
            }

Your content of each cell is something like

<a href="">DAYOFMONTH</a>Title

You are splitting for ">" now, so you got your day of month in tmp[1]. With "search" you search in a string for an regular expression.

EDIT:

<script type="text/javascript">

    function hiliteToday(){
        var nDate = new Date();
        var sDate = nDate.getMonth()+1 + "_" + nDate.getDate();
        document.getElementById(sDate).style.backgroundColor = 'cyan';
    }

window.onload=hiliteToday;

</script>

Change your PHP echo to:

echo "<td id=\"".$month."_".$dayArray["mday"]."\" valign=\"top\"><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a><br/>asdas</td>
";
drgawfsf1069
drgawfsf1069 看编辑。 添加一个id = \“”。$ month。“_”。$ dayArray [“mday”]。“\”到你的回音tds改变hiliteToday()
大约 8 年之前 回复
doutan3192
doutan3192 它确实有效,但我没有注意到它也突出了本月的每个'24'。 例如,今天它是'24',它确实突出显示但是当我去下个月,例如10月时,'24'日期也被突出显示。 我怎样才能解决这个问题? 谢谢。
大约 8 年之前 回复
doujun1495
doujun1495 哇〜! 有效! 你救了我的一天! 非常感谢! :)
大约 8 年之前 回复
duanjingsen7904
duanjingsen7904 看到帖子的编辑。
大约 8 年之前 回复
duande8497
duande8497 但是在你的日历中每天都有一个数字? 所以21日是星期五,而nubmer 21也在日历中?
大约 8 年之前 回复
dongshao8125
dongshao8125 too long
大约 8 年之前 回复
douman6679
douman6679 请添加提醒(tmp); 在var tmp = nTable.rows [i] .cells [n] .innerHTML之后; 并请给出第21天的输出。 但请注意,您会收到42个警报。
大约 8 年之前 回复
duanmei1850
duanmei1850 尝试过,但它仍然无法正常工作。
大约 8 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐