2014-03-27 169 views
0

在這個日曆在PHP中,我試圖根據一週中的月份和日期更改CSS。我需要做一個函數來分隔已經過去的日子和星期六和星期天,並設置類「cal_dayblock」和其餘日子還沒有通過,下一個月他們在課堂cal_dayPHP日曆IF日期與日期

已經嘗試幾件事情和我沒有收到,請幫我在這IF語句

<?php 
$months = date("m"); 
$years = date("Y"); 
$day_count = cal_days_in_month(CAL_GREGORIAN, $months, $years); 


for($i=1; $i<= $day_count; $i++) { 


$date = $months.'/'.$i.'/'.$years; 

if($i >= date('d') && $months >= date('m')) { 
    echo '<div class="cal_day">'; 
    echo '<div class="day_heading">' . $i . '</div>';} 

else { 
    echo '<div class="cal_dayblock">'; 
    echo '<div class="day_heading">' . $i . '</div>'; 

} 

echo '</div>'; 

} 

?> 

編輯:

<script language="javascript" type="text/javascript"> 
    function initialCalendar() { 
var hr = new XMLHttpRequest(); 
var url = "calendar_start.php"; 
var currentTime = new Date(); 
var month = currentTime.getMonth() + 1; 
var year = currentTime.getFullYear(); 
showmonth = month; 
showyear = year; 
var vars = "showmonth="+showmonth+"showyear="+showyear; 
hr.open("POST", url, true); 
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
hr.onreadystatechange = function() { 
    if(hr.readyState == 4 && hr.status == 200) { 
     var return_data = hr.responseText; 
     document.getElementById("showCalendar").innerHTML = return_data; 
    } 
} 
    hr.send(vars); 
    document.getElementById("showCalendar").innerHTML = "Carregando..."; 
    } 

和PHP

 $showmonth = $_POST['showmonth']; 
    $showyear = $_POST['showyear']; 
    $showmonth = preg_replace('#[^0-9/]#i', '', $showmonth); 
    $showyear = preg_replace('#[^0-9/]#i', '', $showyear); 

得到錯誤becouse,可以幫助我解決它?請

回答

1

我需要的功能分離已經過去了,天週六和週日,並設置類「cal_dayblock」

第一部分

日是過去 - 只需使用$date < date('d-m-Y')

第二部分

日是星期六/星期日:您可以使用Dl(小寫的L),以獲得無論是星期一或星期一等獲得一天的名字

您將需要在格式創建的日期得到這個dd-mm-YYYY,因爲strtotime()不喜歡mm/dd/YYYY格式。

一旦你得到了這兩方面的條件,你可以試試這個:

$day_count = date('t'); // get days in current month 

$current_year = date('Y'); 
$current_month = date('m'); 

for($i = 1; $i <= $day_count; $i++) { 
    $date_dmy = date('d-m-Y', strtotime($i . '-' . $current_month . '-' . $current_year)); 
    // get the day name using dd-mm-YYYY format for strtotime 
    $day_name = date('D', strtotime($date_dmy)); 

    echo '<div class="cal_day'; 
    // do your separate logic here - if it's in the past or it's a Saturday or Sunday 
    if($date_dmy < date('d-m-Y') || in_array($day_name, array('Sat', 'Sun'))) 
     echo 'block'; 

    // close the rest of your HTML 
    echo '"><div class="day_heading">' . $i . '</div></div>'; 
} 

編輯:我只是將使用dd-mm-YYYY格式比較這裏,因爲即使在m/d/Y格式比較日期與標準的運營商沒有工作。

如果你想輸出該格式的日期,這樣做:

echo date('m/d/Y', strtotime($date_dmy)); 

編輯2:甚至更​​多收拾這個代碼,你可以這樣做:

for($i = 1; $i <= $day_count; $i++) { 
    $class_name = 'cal_day'; 

    $date_dmy = date('d-m-Y', strtotime($i . '-' . $current_month . '-' . $current_year)); 
    $day_name = date('D', strtotime($date_dmy)); 
    if($date_dmy < date('d-m-Y') || in_array($day_name, array('Sat', 'Sun'))) 
     $class_name .= 'block'; 

    $html = '<div class="%s"><div class="day_heading">%s</div></div>'; 
    echo sprintf($html, $class_name, $i); 
} 

Docs:

+0

太好了!但如果我的月和年變量從javascript date()發佈到php ..我編輯了代碼 – Frank021

+0

用'$ showyear'和'$ showmonth'替換'$ current_year'和'$ current_month' - 假設你的AJAX工作正常。 –

+0

非常感謝 – Frank021