UIDatePicker - minDate& maxDate from php Var [英] UIDatePicker - minDate & maxDate from php Var
问题描述
我有一些php查询数据库以获取一些日期:
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]);
}
?>
然后我尝试使用jquery将最小和最大日期分配给uidatepicker。
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())
}
)
但是尝试使用php vars实现相同的方法不起作用:
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和$ maxDate是类似于
$minDate and $maxDate are arrays that look something like
[yyyy|mm|dd]
当回显它们时,它们包含正确的值...这个问题在jQuery ...可能我试图创建一个Date ...但是我的语法究竟是错的方式?我看不到我做错了什么...
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...
推荐答案
你的数组数据可能被放入javascript Date()构造函数在错误的顺序。假设您的日期字符串的格式为YYYY-MM-DD ,您的print_r()您的$ maxdate数组您应该看到这样的:
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
)
请注意,这个数组的索引是0 => year,1 =>月,2 =>天。所以你需要修改你的Date()构造函数来将这些值与这些构造函数输入参数进行对齐:
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>
这篇关于UIDatePicker - minDate& maxDate from php Var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!