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

时间:2012-09-21 07:46:26

标签: php javascript calendar

我有一个生成日历的PHP代码。我正在尝试在以下代码中突出显示当前日期:

<?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>\n";
foreach ($days as $day) {
    echo "<td style=\"background-color: #CCCCCC; text-align: center; width: 14%\">
          <strong>$day</strong></td>\n";
}

for ($count=0; $count < (6*7); $count++) {
    $dayArray = getdate($start);
    if (($count % 7) == 0) {
        if ($dayArray["mon"] != $month) {
            break;
        } else {
            echo "</tr><tr>\n";
        }
    }
    if ($count < $firstDayArray["wday"] || $dayArray["mon"] != $month) {
        echo "<td>&nbsp;</td>\n";
    } 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>\n";

        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>

但它似乎不起作用。 javasript函数hiliteToday()应该可以正常工作,因为我在<table id='currMonth'>绑定了它,但我没有得到任何结果。我究竟做错了什么?请帮我。提前致谢!

编辑:以下是日历的样子:

enter image description here

1 个答案:

答案 0 :(得分:0)

如果表格单元格具有当前日期,则使用if(tmp [0] == nDate)进行检查。在今天的例子“21”中。 但是你永远不会只为你的桌子细胞设置几天。

将您的第二个for循环更改为:

         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'}
            }

每个单元格的内容类似于

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

你正在拆分“&gt;”现在,所以你在tmp [1]中获得了一个月的日子。使用“搜索”,您可以在字符串中搜索正则表达式。

编辑:

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

将PHP回显更改为:

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>\n";