如何在jQuery Datepicker中插入PHP值

时间:2012-11-14 04:13:28

标签: php jquery datepicker

我有以下代码来设置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);

    }
});

如果今天的日期是11/14/2012,则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') ?>);

BTW,我正在使用codeigniter会话。

这很好但问题是datepicker中的最小日期未设置为11/16/2012,而是设置为11/2/2012。

任何人都知道如何正确地将PHP值插入jQuery?

1 个答案:

答案 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') ?>);