简体   繁体   English

获取使用php / mysql预订的日期

[英]Get dates that are booked using php/mysql

I have a table in mysql that holds a start and end date. 我在mysql中有一个表,其中包含开始日期和结束日期。 These start and end date are for when a hotel is booked. 这些开始和结束日期适用于预订酒店的情况。 What I need to do is show a calendar that shows the dates that are booked and the dates that are available for the current month. 我需要做的是显示一个日历,显示预订的日期和当月可用的日期。 How would I find out what days are booked for the current month? 我怎样才能知道当月预订的天数? I could loop through each day of the current month but that would mean 30 or 31 queries, is there a better, more optimized way to find out what days are booked for the month so I can color code the days on a calendar? 我可以循环浏览当月的每一天,但这意味着30或31个查询,是否有更好,更优化的方式来查找当月预订的天数,以便我可以在日历上对日期进行颜色编码?

The table structure is this: 表结构是这样的:

hotelid int(11) hotelid int(11)

startdate (date) startdate(日期)

enddate (date) 结束日期

Query Request: 查询请求:

SELECT * FROM hotel_book WHERE hotelid = 1 AND DATE_FORMAT(startdate,'%Y-%m') >= '2012-08' AND DATE_FORMAT(enddate,'%Y-%m') <= '2012-08'

Im sure I could just pass it 2012-08-01 and 2012-08-31 so Im not processing the date format on each check. 我确定我可以通过它2012-08-01和2012-08-31所以我不在每张支票上处理日期格式。 But just for example purposes. 但仅仅是出于示例目的。

Another approach to this would be to allow your calendar to accept start and end dates and render each day that is booked within the calendar logic. 另一种方法是允许您的日历接受开始日期和结束日期,并呈现在日历逻辑中预订的每一天。 If the calendar uses javascript rather than PHP or MySQL impact on your server(s) would be minimal. 如果日历使用javascript而不是PHP或MySQL对您的服务器的影响将是最小的。 Either way you end up looping, it just depends on where you want that to happen. 无论哪种方式,你最终都会循环,它只取决于你想要发生的地方。

So first get the hotel id. 所以首先得到酒店的身份。 Then do a query to get all start and end dates respective to that hotel id. 然后执行查询以获取与该酒店ID相对应的所有开始日期和结束日期。 I suggest you have a second field where you calculate the difference: 我建议你有第二个字段来计算差异:

If someone has booked 1st to the 3rd then the 2nd is booked as well, so you would have to make a function that calculates the difference then outputs the 'in between' fields. 如果有人预订了第1到第3,那么第2个也被预订,所以你必须创建一个计算差异的函数,然后输出'在'之间'字段。 So it would have to first get the start number then add +1 days every time until the end date. 所以它必须首先得到起始编号,然后每次增加+1天直到结束日期。 So it would end up like array(1,2,3) 所以它最终会像数组(1,2,3)

Then you can take this information and put it into a booked array for a jquery or ajax calendar (plenty available online). 然后,您可以获取此信息并将其放入已预订的数组中以获取jquery或ajax日历(可在线获取)。 Presumably once a date is booked it cannot be booked again or else you would have duplicate values of 1,2,3 - so I'm guessing the hotel only has one room avail or something. 据推测,一旦日期被预订,它就不能再次预订,否则你会有1,2,3的重复值 - 所以我猜这家酒店只有一个房间可用或什么的。

Here is the code to get the dates you want. 这是获取所需日期的代码。 You'll be able to show them on your calendar regarding to it's logic. 您将能够在日历上显示它们的逻辑。 Note that i used custom class for database select. 请注意,我使用自定义类进行数据库选择。

$id=$_POST['id'];
$days=$db->select("reservations","room_id=".$id);
$listofdays=array();
foreach ($days $day) {
$start=$day['start'];
$end=$day['end'];
$liste=array(); //This array will be filled with dates between start and end
$liste=date_range($start, $end, $step = '+1 day', $output_format = 'm-d-Y'     );
foreach ($liste as $key => $list) {
$listofdays[] = $list;

}
}

//date_range function below: // date_range函数如下:

    function date_range($first, $last, $step = '+1 day', $output_format = 'm/d/Y' ) {

$dates = array();
$current = strtotime($first);
$last = strtotime($last);

while( $current <= $last ) {

    $dates[] = date($output_format, $current);
    $current = strtotime($step, $current);
}

return $dates;

}; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM