2012-11-14 36 views
1

我有以下代碼來設置datepicker的最小日期。如何在jQuery中插入PHP值Datepicker

$("#sd").datepicker({ 
    // before datepicker opens run this function 
    beforeShow: function(){ 
     // this gets today's date  
     var theDate = new Date(); 
     // sets "theDate" 2 days ahead of today 
     theDate.setDate(theDate.getDate() + 2); 
     // set min date as 2 days from today 
     $(this).datepicker('option','minDate',theDate);   
    }, 
    // When datepicker for start date closes run this function 
    onClose: function(){ 
     // this gets the selected start date   
     var theDate = new Date($(this).datepicker('getDate')); 
     // this sets "theDate" 1 day forward of start date 
     theDate.setDate(theDate.getDate() + 1); 
     // set min date for the end date as one day after start date 
     $('#ed').datepicker('option','minDate',theDate); 

    } 
}); 

如果今天的日期是2012年11月14日,那麼datepicker的值是11/16/2012。但是,由於日期應該基於服務器的日期時間,所以我不能使用JavaScript日期,因爲它會獲取計算機的當前日期而不是服務器的日期。

所以我所做的就是將其更改爲下面的代碼:

$("#sd").datepicker({ 
    // before datepicker opens run this function 
    beforeShow: function(){ 
     // this gets today's date  
     var theDate = new Date(); 
     // sets "theDate" 2 days ahead of today 
     //theDate.setDate(theDate.getDate() + 2); 
     theDate.setDate(<?php echo $this->session->userdata('checkin') ?>); 
     // set min date as 2 days from today 
     $(this).datepicker('option','minDate',theDate);   
    }, 
    // When datepicker for start date closes run this function 
    onClose: function(){ 
     // this gets the selected start date   
     var theDate = new Date($(this).datepicker('getDate')); 
     // this sets "theDate" 1 day forward of start date 
     theDate.setDate(theDate.getDate() + 1); 
     // set min date for the end date as one day after start date 
     $('#ed').datepicker('option','minDate',theDate); 

    } 
}); 

請在該行一起來看看:

theDate.setDate(<?php echo $this->session->userdata('checkin') ?>); 

順便說一句,我使用笨會議。

這很好,但問題是datepicker中的最小日期沒有設置爲2012年11月16日,而是設置爲11/2/2012。

任何人都知道如何正確地將PHP值插入到jQuery中?

+0

你肯定值$這 - >會話級>用戶數據( '籤')實際上不是2012年11月2日..? –

+0

我相信價值是11/16/2012 .. – jaypabs

+0

根據你在這裏顯示的東西,似乎並非如此。你只是迴應這個價值,並且你正在獲得一個價值,所以它必須是我所看到的存儲在該會話變量中的值。 如果您將整個會話數組轉儲到屏幕上,該怎麼辦?那麼你會得到什麼? –

回答

0

我已經找到了答案。這裏是:

$("#sd").datepicker({ 
    // before datepicker opens run this function 
    beforeShow: function(){ 
     // this gets today's date  
     var theDate = new Date(); 
     theDate.setDate(<?php echo date('d', strtotime($this->session->userdata('checkin'))) ?>); 
     // set min date as 2 days from today 
     $(this).datepicker('option','minDate',theDate);   
    }, 
    // When datepicker for start date closes run this function 
    onClose: function(){ 
     // this gets the selected start date   
     var theDate = new Date($(this).datepicker('getDate')); 
     // this sets "theDate" 1 day forward of start date 
     theDate.setDate(theDate.getDate() + 1); 
     // set min date for the end date as one day after start date 
     $('#ed').datepicker('option','minDate',theDate); 

    } 
}); 

它應該只提取一天,而不是整個日期。

theDate.setDate(<?php echo date('d', strtotime($this->session->userdata('checkin'))) ?>); 

而不是

theDate.setDate(<?php echo $this->session->userdata('checkin') ?>); 
相關問題