日期从php日历中选择日期

时间:2016-12-13 14:15:34

标签: php date datepicker

我们的PHP日历有问题:我们想选择某个日期,例如。 2016年12月15日,改变背景颜色开始!

以下是代码:

<!DOCTYPE HTML>
<?php
    $date = time();
    date_default_timezone_set("Europe/Amsterdam");

    $day = date('d', $date);
    $month = date('m', $date);
    $year = date('y', $date);

    $first_day = mktime( 0, 0, 0, $month, 1, $year);
    $title = date('F', $first_day);
    $day_of_week = date('D', $first_day);

    switch ($day_of_week){
        case "Mon": $blank = 0; break;
        case "Tue": $blank = 1; break;
        case "Wed": $blank = 2; break;
        case "Thu": $blank = 3; break;
        case "Fri": $blank = 4; break;
        case "Sat": $blank = 5; break;
        case "Sun": $blank = 6; break;
    }

    $days_in_month = cal_days_in_month(0, $month, $year);

    echo "<table border=6 width=394>";
    echo "<tr><th colspan=60> $title $year </th></tr>";
    echo "<tr><td width=62>Monday</td><td width=62>Tuesday</td><td width=62>Wednesday</td><td width=62>Thursday</td><td width=62>Friday</td><td width=62>Saturday</td><td width=62>Sunday</td></tr>";

    $day_count = 1;

    echo "<tr>";

    while ($blank > 0 ) {
        echo "<td></td>";
        $blank = $blank-1;
        $day_count++;
    }

    $day_num = 1;

    // Deze while-loop zorgt ervoor dat de tabel naar de volgende rij gaat na 7 dagen.
    while ($day_num <= $days_in_month) {
        echo "<td>$day_num</td>"; // if statement hier in zetten om achtergrond te veranderen
        $day_num++;
        $day_count++;          

        if  ($day_count > 7){
            echo "<tr></tr>";
            $day_count = 1;
        }
    }

    while ($day_count > 1 && $day_count <= 7){
        echo "<td> </td>";
        $day_count++;
    }

    echo "</tr></table>";

?>

1 个答案:

答案 0 :(得分:0)

试试这个

if($day_num == '15'){$bg = "style='background-color:red'";}else{$bg="";} 

如果你想与今天匹配

    if($day_num == $day){$bg = "style='background-color:red'";}else{$bg="";}

把它放在这里

while($ day_num&lt; = $ days_in_month){

    if($day_num == $day){$bg = "style='background-color:red'";}else{$bg="";}




   echo "<td $bg>$day_num</td>"; // if statement hier in zetten om achtergrond te veranderen
    $day_num++;
    $day_count++;          


    if  ($day_count > 7){
        echo "<tr></tr>";
        $day_count = 1;
    }

}