dongxun2903 2014-10-10 12:32
浏览 51


I'm trying to set the default date in datepicker with a variable I pass into the html from PHP. Here's a simplified version of both my control file and form:

control file:

  function render($template, $values = []) {        
      // extract variables into local scope

      // render template

  $default_date = date("m/d/Y") ;       
  $default_date = strtotime($default_date);
  $default_date = $default_date + 604800;
  $default_date = date("Y,m-1,d",$default_date);
  render("index_month2_form.php",['default_date'=> $default_date]);

and here is the form:

<!doctype html>
<html lang="en">
  <?php print "$default_date"; ?> 
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="jqueryui/css/swanky-purse/jquery-ui-1.10.4.custom.css">
  <script src="/jqueryui/jquery-1.11.1.min.js"> </script>
  <script src="jqueryui/js/jquery-ui-1.10.4.custom.min.js"></script>  
  <script type="text/javascript">
      $(function() { 
       $("#mydate").datepicker ({
          onSelect: function(dateText, inst) {
                 var dateAsString = dateText;     
                 var date = $('#mydate').val();       
       //.datepicker("setDate",new Date());  
       .datepicker("setDate",new Date(2014,10-1,17));  
  <p>Date: <input type="text" id="mydate"></p> 

If I use the commented line for setDate I get the current date. If I use the line I have I get the date 7 days forward. When I print $default_date at the top of the form I get 2014,10-1,17 but I can't firgure out of way to pass this into the script. Others have suggested using

  • 写回答

6条回答 默认 最新

  • douzhantanju1849 2014-10-10 12:58

    using echo is the way to go.
    I would rather put it in the script rather than the input
    I assume $default_date === "2014,10-1,17"

    $(function() { 
           $("#mydate").datepicker ({
              onSelect: function(dateText, inst) {
                     var dateAsString = dateText;     
                     var date = $('#mydate').val();       
           //.datepicker("setDate",new Date());  
           .datepicker("setDate",new Date(
         $date = explode(',', $default_date);
         echo $date[0] .  ',' . $date[1] . ',' . $date[2]; 

    As rss81 noticed it, destructuring the string to rebuild the exact same string is quite dumb. I dont know what I was thinking...
    Nevertheless I'll let it like this for educational purpose.

    exploding the string enables us to get an array of the string of each chunk separated by a coma. You could use it to reorder the string. For instance if you wanted to transform "2014,10-1,17" to "10-1,2014,17" that would be done by echo $date[1].','.$date[0].','.$date[2]

    Echo enables us to output the html page as we like, making it dynamic. So here we are preprocessing the date argument of the .datepicker() by php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥33 集成BiLSTM模型和GCN模型时,前模型的输出不匹配后模型的输入
  • ¥15 关于液体厚度提取,和铺展轨迹数据化处理
  • ¥20 对方微信是wxid如何添加成好友?或者转换成微信号?
  • ¥15 yolov5实验数据好,但是自己拍摄的照片检测结果差
  • ¥15 做过dali2.0认证的技术,
  • ¥15 一道python难题
  • ¥15 matlab 免疫算法
  • ¥50 可视化界面构建,实现信息模块功能
  • ¥15 基于bert-base-chinese的中文文本处理
  • ¥15 高价求抖音抓包技术,抖音抓包无网络问题