简体   繁体   中英

Get date range for multiple from/to dates

I have this php code that I'am using to disable jquery ui datepicker date range in calendar. It selects every date in given range and echoes it. Problem is that it only echoes one from/to range but I need that for every from/to column:

$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++;
}

You are getting every column but you're overwriting each one and left with the last one selected. You should build an array of your results and then loop over those. I'm not sure 100% what you're going for, but something like this:

$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++;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM