获取多个自/至日期的日期范围

时间:2014-02-15 18:20:53

标签: php mysql

我有这个用于在日历中禁用jquery ui datepicker日期范围的PHP代码。 它选择给定范围内的每个日期并回显它。问题是它只回显了一个从/到范围但我需要每个从/到列:

$a = "select from_date,to_date from ads";

$rez = mysql_query($a) or die(mysql_error());
$c = mysql_num_rows($rez);

while ($re = mysql_fetch_array($rez))
{
    $from = $re["from_date"];
    $to = $re["to_date"];
}
$start=strtotime($from);
$s=date("Y-m-d",$start);
$diff = abs(strtotime($to) - strtotime($s)); 


$yrs   = floor($diff / (365*60*60*24)); 
$mnth  = floor(($diff - $yrs * 365*60*60*24) / (30*60*60*24)); 
$days    = floor(($diff - $yrs * 365*60*60*24 - $mnth*30*60*60*24)/ (60*60*24));
$t=1;



$count = 0;
while($t <= $days){
if ($count++ > 0) echo ", ";
echo(json_encode($s));
$date = strtotime("+1 day", strtotime($s));
$s=date("Y-m-d", $date);
$t++;
}

1 个答案:

答案 0 :(得分:0)

您正在获取每一列,但是您要覆盖每一列,并选择最后一列。您应该构建一个结果数组,然后循环遍历这些结果。我不确定100%你想要什么,但是这样的事情:

$result = array();
while ($re = mysql_fetch_array($rez))
{
    $from_date = $re["from_date"];
    $to_date = $re["to_date"];
    $start=strtotime($from);
    $s=date("Y-m-d",$start);
    $result[] = array($to_date, $s);
    ## Each element of $result is an array with two elements
    ## So $result[0][0] is the first to_date
}

## Now loop over each $result and process the to_date and $s value

foreach($result as $re){
    //echo $re[0] .' is this result\'s $to_date value';
    //echo $re[1] .' is this result\'s $s value';
    $to_date = $re[0];
    $s = $re[1];

    $diff = abs(strtotime($to_date) - strtotime($s)); 


    $yrs   = floor($diff / (365*60*60*24)); 
    $mnth  = floor(($diff - $yrs * 365*60*60*24) / (30*60*60*24)); 
    $days    = floor(($diff - $yrs * 365*60*60*24 - $mnth*30*60*60*24)/ (60*60*24));
    $t=1;

    $count = 0;
    while($t <= $days){
      if ($count++ > 0) echo ", ";
      echo(json_encode($s));
      $date = strtotime("+1 day", strtotime($s));
      $s=date("Y-m-d", $date);
      $t++;
    }
}