开发者

Looping through dates until a free one is found

I have a function which checks my database to see if a date exists, if it does exist, i want to display the next date which isnt in the database.

Is this possible?

My function returns 1 if there is a date in the database and 0 if there isnt, im using codeigniter, but not using any built in functions.

Its basically an availability checker, it allows us to input many different dates in the database, so calling my function i use

$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y'));

The i use a if statement to check if the first time it runs it returns a value, this is how i have it

if($availcheck > 0){
    // loop through the next dates and run the function again to see if it returns 0
} else {
    echo 'available now';
}

I guess i would add 1 to the current date, check that one, then add another 1 and check tha开发者_如何学Ct and so on.

Im just not sure how.

Cheers,


if i understand you correct , your problem is adding the day ? if so i would suggest using the epoch or unix time so convert the date to unix time using mktime than just add 1 day in seconds (24*60*60) and then convert back to d/m/y format. you can use the date function.

$date = time();  // get current timestamp
while ($availcheck)  // while date IS found in database
{
$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y',$date));
$date = $date + (24*60*60);  // add one day
}
$date = $date - (24*60*60);  // reduce one day
echo date('d/m/Y',$date);  // prints the first date that is not in the DB


This SQL code could work for me. $today = date("Y-m-d"); //today

$sql = "SELECT date FROM calendar WHERE date>'{$today}' AND date<='2100-12-31' AND date='0000-00-00' LIMIT 1";

Since you can't determine the ending date, 2100 could be for testing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜