I'm trying to insert data with a date that doesn't currently exist in my database. Some parents are getting the same time and date on their appointment as other parents. How would I have the data insert a random date in a maximum of 7 days?
Example:
It'll generate a date with an existing date.
Dec 4th 2007 11pm
Dec 4th 2007 9pm - problem
Dec 4th 2007 9pm - problem
Dec 4th 2007 9pm - problem
Dec 4th 2007 9pm
Dec 4th 2007 8pm
Dec 4th 2007 7pm
Dec 4th 2007 3am - problem
Dec 4th 2007 3am
Dec 4th 2007 2am - problem
Dec 4th 2007 2am
I currently have this to check if it'll run into duplicate dates but I don't know what to do to keep searching for available dates if duplicate exist.
$dateformat = "M jS Y ga";
//pull all the dates from now and up
$datecheck = mysql_query("SELECT * FROM `" . $db_ext . "_item` WHERE '" . time() . "'<=i_time ORDER BY i_time DESC");
while ($row = mysql_fetch_array($datecheck)) {
//pull all the dates and see if the new generated dates exist with this one
$dates .= "" . date($dateformat, $row[i_time]) . "";
}
$appointment = mysql_query("SELECT * FROM `" . $db_ext . "_item` ORDER BY i_time DESC");
echo "<table>";
while ($row = mysql_fetch_array($appointment)) {
$rand_day_1 = rand(0, 7);
$rand_day_2 = "+$rand_day_1 day";
$rand_hour_1 = rand(0, 14);
$rand_time_2 = "+$rand_hour_1 hour";
//unixtime
$time_current = strtotime("$rand_day_2 $rand_time_2");
//readable time
$newdate = "" . date($dateformat, $time_current) . "";
$finddate = strpos($dates, $newdate);
if ($finddate === false) {
//date is available, use this date
$datesearch = "Available, now use this: $newdate";
} else {
//date already exist
//#######################
// Since the date isn't available, how would I keep generating numbers until one is available?
//#######################
$datesearch = "Not Available, generate until one is: $newdate";
}
echo "<tr><td>$datesearch</td></tr>";
}
echo "</table>";