开发者

UIDatePicker - minDate & maxDate from php Var

开发者 https://www.devze.com 2023-03-30 11:33 出处:网络
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\";

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 = e开发者_开发百科xplode("-", 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...


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号