UIDatePicker - minDate & maxDate from php Var

喜你入骨 提交于 2019-12-02 08:36:42

问题


I have some php which queries a database to get some dates:

<?php
//connect to db etc
$sql="SELECT date FROM entries ORDER BY date ASC"; 
$result = mysql_query($sql);
if($result){
  $entryDates = array();
  while ($row=mysql_fetch_array($result)) { 
    $entryDates[] = $row['date'];
  }
  $minDate = explode("-", $entryDates[0]);
  $minDate[1] -= 1;
  $maxDate = explode("-", end($entryDates));
  $maxDate[1] -= 1;
  echo($maxDate[1]);
}

?>

I then try and assign the min and max dates to a uidatepicker using jquery.

Originally I did this and it worked:

var theDate = new Date();
      $( "#datepicker" ).datepicker(
          {        
          dateFormat: 'dd-mm-yy',
          maxDate: new Date(theDate.getFullYear(), theDate.getMonth(), theDate.getDate())

      }
      )

However trying to implement the same using the php vars doesn't work:

$( "#datepicker" ).datepicker(
          {        
          dateFormat: 'dd-mm-yy',
          maxDate: new Date(<?php echo($maxDate[2]);?>,<?php echo($maxDate[1]);?>,<?php echo($maxDate[0]); ?>)
          minDate: new Date(<?php echo($minDate[2]);?>,<?php echo($minDate[1]);?>,<?php echo($minDate[0]); ?>)
      }
      );

I am trying to use the following date constructor:

new Date(year, month, day, hours, minutes, seconds, milliseconds)

$minDate and $maxDate are arrays that look something like

[yyyy|mm|dd]

When echoing them they contain the correct values..so the issue lies within jQuery...probably the way I am trying to create a Date...but what exactly is wrong with my syntax? I cannot see what I am doing wrong...


回答1:


Your array data is probably being placed into the javascript Date() constructor in the wrong order. Assuming your date strings are in the format YYYY-MM-DD and you print_r() your $maxdate array you should see something like this:

Array
(
    [0] => 2011
    [1] => 0
    [2] => 01
)

Notice that this array's indices are in the order 0=>year, 1=>month, 2=>day. So you need to modify your Date() constructors to align those values with the constructors input parameters like this:

<script type="text/javascript">
$( "#datepicker" ).datepicker({        
    dateFormat: 'dd-mm-yy',
    maxDate: new Date(<?php echo($maxDate[0]);?>,<?php echo($maxDate[1]);?>,<?php echo($maxDate[2]); ?>),
    minDate: new Date(<?php echo($minDate[0]);?>,<?php echo($minDate[1]);?>,<?php echo($minDate[2]); ?>)
});
</script>


来源:https://stackoverflow.com/questions/7168472/uidatepicker-mindate-maxdate-from-php-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!