I know this one is strange. I have a table called schedule with a column called Date with the column type being DATE. I have dates (in YYYY-MM-DD format) from 2012-11-01 to 2013-01-02 in this column. What’s happening is if I enter a date, any date, that has 2012-12-01 anywhere in between, it restarts the loop. So if I select 2012-11-15 to 2012-12-15 it will print from Novemeber 15th to December 1st, but then after December 1st it starts printing again at November 15th. If I select 2012-11-15 to 2012-11-30 or 2012-12-02 to 2012-12-30 everything works fine, it’s just when that particular date is involved that it causes problems. Here is my code:
<?php
////////////////////////////////////////////////////////
//////First set the date range that we want to use//////
////////////////////////////////////////////////////////
if(isset($_POST['from']) && ($_POST['from'] != NULL))
{
$startDate = $_POST['from'];
echo "<script>alert (\"startDate: " . $startDate . "\")</script>";
}
else
{
//Default date is Today
$startDate = date("Y-m-d");
echo "<script>alert (\"startDate: " . $startDate . "\")</script>";
}
if(isset($_POST['to']) && ($_POST['to'] != NULL))
{
$endDate = $_POST['to'];
echo "<script>alert (\"endDate: " . $endDate . "\")</script>";
}
else
{
//Default day is one month from today
$endDate = date("Y-m-d", strtotime("+1 month"));
echo "<script>alert (\"endDate: " . $endDate . "\")</script>";
}
//////////////////////////////////////////////////////////////////////////////////////
//////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 "<script>alert (\"Total Days: " . $total_days . "\")</script>";
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////Then we're going to get the date range specified from the schedule table and print the results//////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Select the dates from the schedule table, limited to the total amount days.
$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 date from the schedule table [' . $mysqli->error . ']');
}
//Loop through the results while a result is being returned.
while($row = $result_date_query->fetch_assoc())
{
//First format and then print the all the dates selected.
echo "<td class=\"schedule_date_cell\">" . date('M j', strtotime($row['Date'])) . "</td>";
}
//Free the result.
$result_date_query->free();
?>
I am at a loss. I’ve added in some echo statements to show what’s going on and everything is as expected. I just don’t understand how all dates before and all dates after work perfectly, but anything that has Dec 1st in it makes it restart the loop. I’m not sure if it’s relevant, but there are multiples of the same dates in the column, which is why I have set LIMIT in the query.
I’m hoping someone can crack this case wide open as I’m dying to know the answer!
**EDIT: **I tried this same query from within PHPmyadmin as suggested in the comments, and it produces the same results. I also tried removing the LIMIT as well as changing from single quotes '2012-11-01' to double quotes "2012-11-01" with still the same results.
**EDIT2: **For each date in the Date column, there are 92 instances of each date. So, for example, there are 92 records of 2012-12-01 and 92 records of 2012-12-02 and so on. The interesting thing is, when I remove LIMIT from the query and use the date range of 2012-11-30 to 2012-12-15 (Nov 30th to Dec 15th) is prints Nov 29th, Nov 30th, Dec 1st then repeats those three dates again and again, 92 times! Then after that it starts to print the rest of the days Dec 2nd, Dec 3rd, Dec 4th …Dec 15th and then repeats THOSE dates again and again, 92 times! This just proves that for some reason the loop is restarting at Dec 1st because if I enter a date range of Nov 1st to Nov 3rd it prints all dates in the range before repeating itself 92 times. The reason for the LIMIT was so to stop it from returning all instances of each date, so I would only get Nov 1st to Nov 3rd one time and not 92 times, but when the limit is removed we can see that Dec 1st is causing some weird issues.
**EDIT3: **I created a new table schedule2 identical to schedule and entered the same date range of 2012-11-01 to 2013-01-02, but this time with only one instance of each. Then I ran my query again and it works perfectly, in any date combination. So obviously the problem is that there is more than one instance of this date in the schedule table, but I can’t figure out WHY it’s a problem. I don’t understand what this date is having an affect on the while loop. And before you suggest it, yes, I do need to have multiple instances of the same date in the table 😉
The fact that it loops at Dec 1st is for the most part just a coincidence. I don’t know the specifics of how MySQL works internally, but if you don’t give it an order, then it will apply whatever order it feels like to the data when you request it.
Combine this with the fact that you have duplicate dates, and then you end up with the situation have you described spontaneously happening for no apparent reason.
Since you want each date exactly once, and you want them to appear in order chronologically, then you need to tell MySQL you want those two things when you make the query. As such, add
DISTINCTANDORDER BYcommands: