Php jQuery日历中的JSON日期范围

Php jQuery日历中的JSON日期范围,php,ajax,json,Php,Ajax,Json,我有一个场景,我从jQuery日历中选择一个日期,然后通过ajax迭代JSON文件。我的问题是由于使用了日期范围,例如,请参见startdate和enddate: 从上面的代码段可以看出,startdate和enddate是一个范围。我想知道最好的方法是尝试从JSON文件中的某个范围内选择一个日期 我考虑过使用PHP分解来分解传递给ajax的日期,然后通过比较分解后的参数和startdate和enddate中的数据返回到原始页面。我想知道分解日期,然后在开始日期和结束日期之间迭代,看看是否有匹配

我有一个场景,我从jQuery日历中选择一个日期,然后通过ajax迭代JSON文件。我的问题是由于使用了日期范围,例如,请参见startdate和enddate:

从上面的代码段可以看出,startdate和enddate是一个范围。我想知道最好的方法是尝试从JSON文件中的某个范围内选择一个日期

我考虑过使用PHP分解来分解传递给ajax的日期,然后通过比较分解后的参数和startdate和enddate中的数据返回到原始页面。我想知道分解日期,然后在开始日期和结束日期之间迭代,看看是否有匹配的日期会起作用吗


如果我的上述想法行不通,还有什么其他方法可以实现呢?

你为什么不试试你的想法呢?听起来是个好的开始。但不使用explode可以节省一些精力。PHP有一些有用的内置日期/时间函数,特别是strotime,它可以将几乎任何格式的字符串日期转换为Unix时间戳。这只是一个整数,可以很容易地与其他时间戳进行比较。看一看:

$startdate;       #read this in from the json, contains e.g. '2013-01-02'
$enddate;         #read this in from the json, contains e.g. '2013-01-06'

#we want to check if this date is within the range
$datetomatch = '2013-01-04';

#get integer timestamps for each date
$starttime = strtotime($startdate);
$endtime = strtotime($enddate);
$matchtime = strtotime($datetomatch);

#check if $matchtime is within the range
if($matchtime >= $starttime && $matchtime <= $endtime) {
    #datetomatch is between $startdate and $enddate
}
因此,对于JSON periods数组中的第一个对象,if语句将为true,因为2013-01-04介于2013-01-02和2013-01-06之间,或者更具体地说,因为时间戳1357279200介于1357106400和1357452000之间。对于周期中的第二个对象,它不是真的


当然,如果您想使用此代码检查多个日期,可以将其包装在函数中。

为什么不试试您的想法?听起来是个好的开始。但不使用explode可以节省一些精力。PHP有一些有用的内置日期/时间函数,特别是strotime,它可以将几乎任何格式的字符串日期转换为Unix时间戳。这只是一个整数,可以很容易地与其他时间戳进行比较。看一看:

$startdate;       #read this in from the json, contains e.g. '2013-01-02'
$enddate;         #read this in from the json, contains e.g. '2013-01-06'

#we want to check if this date is within the range
$datetomatch = '2013-01-04';

#get integer timestamps for each date
$starttime = strtotime($startdate);
$endtime = strtotime($enddate);
$matchtime = strtotime($datetomatch);

#check if $matchtime is within the range
if($matchtime >= $starttime && $matchtime <= $endtime) {
    #datetomatch is between $startdate and $enddate
}
因此,对于JSON periods数组中的第一个对象,if语句将为true,因为2013-01-04介于2013-01-02和2013-01-06之间,或者更具体地说,因为时间戳1357279200介于1357106400和1357452000之间。对于周期中的第二个对象,它不是真的


当然,如果您想使用此代码检查多个日期,可以将其包装在函数中。

谢谢您的帮助!为援助干杯!