0

我有一个名为的表schedule和一个名为Datewhere column type is 的列date。在该列中,我有一个日期范围,目前是从 2012 年 11 月 1 日到 2012 年 11 月 30 日。我有一个小表单,用户可以在其中输入日期范围(输入名称fromto),并且我希望能够将日期范围与数据库中当前的日期进行比较。

这就是我所拥有的:

////////////////////////////////////////////////////////
//////First set the date range that we want to use//////
////////////////////////////////////////////////////////

if(isset($_POST['from']) && ($_POST['from'] != NULL))
    {
    $startDate = $_POST['from'];
    }
else
    {
    //Default date is Today
    $startDate = date("Y-m-d");
    }
if(isset($_POST['to']) && ($_POST['to'] != NULL))
    {
    $endDate = $_POST['to'];
    }
else
    {
    //Default day is one month from today
    $endDate = date("Y-m-d", strtotime("+1 month"));
    }

//////////////////////////////////////////////////////////////////////////////////////
//////Next calculate the total amount of days selected above to use as a limiter//////
//////////////////////////////////////////////////////////////////////////////////////

$dayStart = strtotime($startDate);
$dayEnd = strtotime($endDate);
$total_days = abs($dayEnd - $dayStart) / 86400 +1;

echo "Start Date: " . $startDate . "<br>End Date: " . $endDate . "<br>";
echo "Day Start: " . $dayStart . "<br>Day End: " . $dayEnd . "<br>";
echo "Total Days: " . $total_days . "<br>";

////////////////////////////////////////////////////////////////////////////////////
//////Then we're going to see if the dates selected are in the schedule table//////
////////////////////////////////////////////////////////////////////////////////////

//Select all of the dates currently in the schedule table between the range selected.
$sql = ("SELECT Date FROM schedule WHERE Date BETWEEN '$startDate' AND '$endDate' LIMIT $total_days");

//Run a check on the query to make sure it worked. If it failed then print the error.
if(!$result_date_query = $mysqli->query($sql))
    {
    die('There was an error getting the dates from the schedule table [' . $mysqli->error . ']');
    }

//Set the dates to an array for future use.
// $current_dates = $result_date_query->fetch_assoc();

//Loop through the results while a result is being returned.
while($row = $result_date_query->fetch_assoc())
    {
    echo "Row: " . $row['Date'] . "<br>";
    echo "Start day: " . date('Y-m-d', $dayStart) . "<br>";
    //Set this loop to add 1 day to the Start Date until it reaches the End Date
    for($i = $dayStart; $i <= $dayEnd; $i = strtotime('+1 day', $i))
        {
        $date = date('Y-m-d',$i);
        echo "Loop Start day: " . date('Y-m-d', $dayStart) . "<br>";

        //Run a check to see if any of the dates selected are in the schedule table.
        if($row['Date'] != $date)
            {
            echo "Current Date: " . $row['Date'] . "<br>";
            echo "Date: " . $date . "<br>";
            echo "It appears as though you've selected some dates that are not in the schedule database.<br>Please correct the issue and try again.";
            return;
            }
        }
    }

    //Free the result so something else can use it.
    $result_date_query->free();

正如你所看到的,我已经添加了一些 echo 语句,所以我可以看到正在生成的内容。从我所见,看起来我$row['Date']并没有增加并且保持在同一日期。我最初将它设置为一个变量(目前已被注释掉),但我认为这可能会导致问题。

我创建了日期从 2012-11-01 到 2012-11-15 的表格进行测试,并将所有这些 php 代码输入到 phpfiddle.org,但我无法获得提供的用户名进行连接。

这是链接:PHP Fiddle

在此期间,我将通读文档以尝试找出用户连接问题,我非常感谢您能给我的任何指导或建议。

4

0 回答 0