0

我遇到了一个致命错误。这里缺少什么?提前感谢您的帮助。我在学习。我正在尝试测试某些日期之间的开放日期。

<?php
$query = 'SELECT venueprofile.venuename, venueprofile.capacity, venueprofile.contact,          venueprofile.EmailAddress, venueprofile.tele, venueprofile.st, venueprofile.city,        showdate.show_date, showdateid'
    . ' FROM `venueprofile` '
    . ' INNER JOIN `showdate` ON venueprofile.venueid = showdate.venueid'
    . ' WHERE show_date BETWEEN \'2013-06-15\' AND \'2013-06-25\''
    . ' AND capacity BETWEEN 500 AND 3000 '
    . ' ';


$startDate = '$date1'; 
$enddate = '$date2'; 
$dates = array($startDate); 
while ($startDate != $enddate) { 
$startDate = date('Y-m-d', strtotime($startDate . ' +1 day')); 
$dates[] = date; 
} 
; 


$bookings = array("2013-06-16","2013-06-20");
foreach ($dates as $date) { 
if (in_array($date, $bookings)) { echo ""; } 
else { echo $date . " is Open\n";}}

?>
4

2 回答 2

3
$startDate = '$date1'; 
$enddate = '$date2'; 

这没有按预期工作;单引号防止变量插值。此外,您不需要引号。

$startDate = $date1; 
$enddate = $date2;

此外,我建议更改您的循环条件。

while ($startDate < $enddate) { 

这样做可以防止$startDate > $enddate在循环开始之前出现问题。

最后,以下语句不起作用:

$dates[] = date;

date是一个你很可能没有定义的常数。你可能是这个意思:

$dates[] = $startDate;
于 2013-05-27T03:43:13.427 回答
0

除了 Jack 已经指出的问题之外,在我看来,只使用一个循环并避免构建日期数组会更有效率。

所以是这样的:

$bookings = array("2013-06-16","2013-06-20");

$startDate = $date1; 
$enddate = $date2; 

while ($startDate <= $enddate) { 
  if (!in_array($startDate, $bookings)) {
    echo $startDate . " is Open\n";
  }
  $startDate = date('Y-m-d', strtotime($startDate . ' +1 day')); 
}

或者您是否需要该$dates阵列用于其他目的?

于 2013-05-27T08:42:23.787 回答