0

我正在使用日历,但无法使之前的日期不可用。目前,我的日历有每个日期的链接,当您单击一个日期时,它会在另一个页面上显示该值。但是,我需要全部使所有已经过去的日期不可用。我知道这与代码末尾附近的 if 语句有关,但我无法弄清楚。这是我的代码

<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July", 
"August", "September", "October", "November", "December");
?>
<?php
$cMonth = isset($_REQUEST["month"]) ? $_REQUEST["month"] : date("n");
$cYear = isset($_REQUEST["year"]) ? $_REQUEST["year"] : date("Y");

$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;

if ($prev_month == 0 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
}
?><!DOCTYPE html>
<html>
<head>
<title>Hook Up</title>
</head>
<style type="text/css">
        table {
        border: 1px solid black;
        border-collapse: collapse;
    }
    th {
        border: 1px solid black;
        padding: 6px;
        font-weight: bold;
        background: #ccc;
    }
    td {
        border: 1px solid black;
        padding: 6px;
        vertical-align: top;
        width: 100px;
        }
</style>
<script type="text/javascript">
function eventWindow(url) {
        event_popupWin = window.open(url, 'event',
        'resizable=yes, scrollbars=yes, toolbar=no, width=400, height=400);
        event_popupWin.opener = self;
}
</script>
<body>
<h1>Select a Night Out</h1>
<table width="200">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left">  <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=".                     $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=".  $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a>  </td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo    $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
</tr>
<?php 
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
$today = date('j');
$currentmonth = date('n');
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ){
echo "<tr>";
}
if($i < $startday){
echo ("<td class='cell cell_txt'> </td>");
} else {
if (($i - $startday + 1) == $today && $currentmonth == $cMonth){
echo ("<td class='cell_today cell_txt'>".($i-$startday+ 1)."</td>");
} else {
echo ("<td class='cell cell_txt'><a href=\"hook.php?day=".($i - $startday +   1)."&amp;month=".$cMonth."&amp;year=".$cYear."\">".($i - $startday + 1)."</a></td>");
}
}
if(($i % 7) == 6 ) {
echo "</tr>\n";
}
}
?>
</table>
</td>
</tr>
</table>
</body>
    </html> 

如果有人可以提供帮助,将不胜感激。干杯

4

2 回答 2

0

<!-----use it will surely work -----!>

<?php
                        $monthNames = Array("January", "February", "March", "April", "May", "June", "July",
                            "August", "September", "October", "November", "December");
                        ?>

                        <?php
                        if (!isset($_REQUEST["day"])) $_REQUEST["day"] = date("d");
                        if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
                        if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
                        ?>

                        <?php
                        $cDay = $_REQUEST["day"];
                        $cMonth = $_REQUEST["month"];
                        $cYear = $_REQUEST["year"];

                        $prev_year = $cYear;
                        $next_year = $cYear;
                        $prev_month = $cMonth - 1;
                        $next_month = $cMonth + 1;

                        if ($prev_month == 0) {
                            $prev_month = 12;
                            $prev_year = $cYear - 1;
                        }
                        if ($next_month == 13) {
                            $next_month = 1;
                            $next_year = $cYear + 1;
                        }
                        ?>
                        <div id="calendar_div" name="calendar_div">
                            <div class="table-responsive">
                                <table width="93%" style="border: none">
                                    <tr>
                                        <td>&nbsp;&nbsp;<a
                                                href="<?php echo $_SERVER["PHP_SELF"] . "?month=" . $prev_month . "&year=" . $prev_year; ?>"
                                                style="color:#FFFFFF"> < </a>
                                            <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=" . $next_month . "&year=" . $next_year; ?>"
                                               style="color:#FFFFFF"> > </a></td>
                                        <td style="float: right">
                                            <h3 style="color: #ffffff;">  <?php echo $monthNames[$cMonth - 1] . ' ' . $cYear; ?></strong></h3>
                                        </td>
                                    </tr>
                                </table>

                                <table  class="table">

                                    <tr>
                                        <td style="color:#FFFFFF;border: none"><strong>Sun</strong></td>
                                        <td style="color:#FFFFFF;border: none"><strong>Mon</strong></td>
                                        <td style="color:#FFFFFF;border: none"><strong>Tue</strong></td>
                                        <td style="color:#FFFFFF;border: none"><strong>Wed</strong></td>
                                        <td style="color:#FFFFFF;border: none"><strong>Thr</strong></td>
                                        <td style="color:#FFFFFF;border: none"><strong>Fri</strong></td>
                                        <td style="color:#FFFFFF;border: none"><strong>Sat</strong></td>
                                    </tr>

                                    <?php

                                    $timestamp = mktime(0, 0, 0, $cMonth, 1, $cYear);
                                    $maxday = date("t", $timestamp);
                                    $thismonth = getdate($timestamp);
                                    $currentmonth = date('n');

                                    $startday = $thismonth['wday'];

                                    for ($i = 0; $i < ($maxday + $startday); $i++) {
                                        if (($i % 7) == 0) echo "<tr>\n";
                                        if ($i < $startday) echo "<td style='border: none'></td>\n";
                                        elseif(($i - $startday + 1) == $cDay && $currentmonth == $cMonth ){



                                            echo "<td  style='background-color: #cccccc'>". ($i - $startday + 1) ."</td>";}

                                        else{

                                            echo "<td  style='color: #ffffff; border: none'>". ($i - $startday + 1) ."</td></a>";}

                                        if(($i % 7) == 6 ) echo "</tr>";

                                    }

                                    ?>


                                </table>

于 2015-02-26T11:36:19.050 回答
0

如果您添加以下行

$cDay = isset($_REQUEST["day"]) ? $_REQUEST["day"] : date("d");

在这条线以下

$cYear = isset($_REQUEST["year"]) ? $_REQUEST["year"] : date("Y");

并更改此行

if (($i - $startday + 1) == $today && $currentmonth == $cMonth){

至:

if ((($i - $startday + 1) == $today && $currentmonth == $cMonth) OR ( (($i - $startday + 1) == $cDay) && ($currentmonth == $cMonth))  ){

这将使传入的日期变黑。

如果您想传入多个日期,即选择一个日期 2013-01-18,页面会重新加载并清空 18 日,然后想要选择第二个日期,例如 2013-01-22 并让页面重新加载并清空两个日期18 日和 22 日,您需要将输入更改为数组,并将之前选择的日期添加到隐藏字段中以重新提交。

如果您只想涂黑一个日期,那么代码更改将起作用。

希望这可以帮助。

更新以将所选日期的所有先前日期涂黑:

<?php
    $monthNames = Array("January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December");
    ?>
    <?php
    $sMonth = isset($_REQUEST["smonth"]) ? $_REQUEST["smonth"] : date("n");
    $sYear = isset($_REQUEST["syear"]) ? $_REQUEST["syear"] : date("Y");
    $cMonth = isset($_REQUEST["month"]) ? $_REQUEST["month"] : '';
    $cYear = isset($_REQUEST["year"]) ? $_REQUEST["year"] : '';
    $cDay = isset($_REQUEST["day"]) ? $_REQUEST["day"] : '';

    //echo __line__." Dates in ".$cDay." ".$cMonth.", ".$cYear."<br>";

    $prev_year = $sYear;
    $next_year = $sYear;
    $prev_month = $sMonth - 1;
    $next_month = $sMonth + 1;

    if ($prev_month == 0) {
        $prev_month = 12;
        $prev_year = $sYear - 1;
    }
    if ($next_month == 13) {
        $next_month = 1;
        $next_year = $sYear + 1;
    }
    $nextPrevString = "&amp;month=$cMonth&amp;year=$cYear&amp;day=$cDay";
    $selectString = "&amp;smonth=$sMonth&amp;syear=$sYear";
    ?><!DOCTYPE html>
    <html>
        <head>
            <title>Hook Up</title>
        </head>
        <style type="text/css">
            table {
                border: 1px solid black;
                border-collapse: collapse;
            }
            th {
                border: 1px solid black;
                padding: 6px;
                font-weight: bold;
                background: #ccc;
            }
            td {
                border: 1px solid black;
                padding: 6px;
                vertical-align: top;
                width: 100px;
            }
        </style>
        <script type="text/javascript">
            function eventWindow(url) {
                event_popupWin = window.open(url, 'event', resizable=yes, scrollbars=yes, toolbar=no, width=400, height=400);
                event_popupWin.opener = self;
            }
        </script>
        <body>
            <h1>Select a Night Out</h1>
            <table width="200">
                <tr align="center">
                    <td bgcolor="#999999" style="color:#FFFFFF">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td width="50%" align="left">  <a href="<?php echo $_SERVER["PHP_SELF"] . "?smonth=" . $prev_month . "&syear=" . $prev_year . $nextPrevString; ?>" style="color:#FFFFFF">Previous</a></td>
                                <td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?smonth=" . $next_month . "&syear=" . $next_year . $nextPrevString; ?>" style="color:#FFFFFF">Next</a>  </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <table width="100%" border="0" cellpadding="2" cellspacing="2">
                            <tr align="center">
                                <td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$sMonth - 1] . ' ' . $sYear; ?></strong></td>
                            </tr>
                            <tr>
                                <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
                                <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
                                <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
                                <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
                                <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
                                <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
                                <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
                            </tr>
                            <?php
                            $timestamp = mktime(0, 0, 0, $sMonth, 1, $sYear);
                            if ($cDay != '') {
                                $selectedDate = mktime(0, 0, 0, $cMonth, $cDay, $cYear);
                            } else {
                                $selectedDate = 0;
                            }
                            $maxday = date("t", $timestamp);
                            $thismonth = getdate($timestamp);
                            $startday = $thismonth['wday'];
                            $today = date('j');
                            $currentmonth = date('n');

                            for ($i = 0; $i < ($maxday + $startday); $i++) {
                                if (($i % 7) == 0) {
                                    echo "<tr>";
                                }
                                if ($i < $startday) {
                                    echo ("<td class='cell cell_txt'> </td>");
                                } else {
                                    $testDate = mktime(0, 0, 0, $sMonth, $i - $startday + 1, $sYear);
                                    if ($testDate < $selectedDate) {
                                        echo ("<td class='cell_today cell_txt'>" . ($i - $startday + 1) . "</td>");
                                    } else {
                                        echo ("<td class='cell cell_txt'><a href=\"" . $_SERVER['PHP_SELF'] . "?day=" . ($i - $startday + 1) . "&amp;month=" . $sMonth . "&amp;year=" . $sYear . $selectString . "\">" . ($i - $startday + 1) . "</a></td>");
                                    }
                                }
                                if (($i % 7) == 6) {
                                    echo "</tr>\n";
                                }
                            }
                            ?>
                        </table>
                    </td>
                </tr>
            </table>
        </body>
    </html> 
于 2013-01-27T11:36:34.553 回答