你好,这是我今年第一次问问题。请帮帮我,我求求你。
我有一个具有开始日期和结束日期的预约表,现在我想遍历它们,并注意它们与我当前日期预订的不同间隔。在我得到所有不同的区间后,我也想遍历到那个区间,并得到可能的或最近的日期。
最近的日期应该是0(enddate == startdate),因为登记时间是固定的。
假设在同一个房间里已经预订了一堆日期。
bookingstart = '2013-02-5 14:00:00' bookingend = '2013-02-7 14:00:00'
bookingstart = '2013-02-13 14:00:00' bookingend = '2013-02-16 14:00:00'
bookingstart = '2013-01-22 14:00:00' bookingend = '2013-02-23 14:00:00'现在初始代码是这样的:
$newbookingstart = '2013-01-24 14:00:00'
$newbookingend = '2013-01-27 14:00:00';
$sql = mysql_query("SELECT enddate, startdate FROM reservation WHERE rmlocation = 'RM24'");
if (!$sql) {
exit('Error: ' . mysql_error());
}
$diffInterval = array();
while ($row = mysql_fetch_array($sql)) {
if ($row['enddate'] >= $newbookingstart && $newbookingend <= $row['stardate']) {
$diffInterval .= $row['startdate'] . $row['enddate'] . $row['rmlocation'];
}
}
//Get the $diffInterval, traverse and get the lowest date interval posible我的问题是如何正确地使用这些代码,特别是在获取数据库中的数据时。如何改进我在同一个房间中搜索所有其他结束日期和开始日期。
发布于 2013-01-22 07:58:56
如果你使用的是MySQL DateTime字段,它应该能够将你从DB获取的字符串转换成你可以用来计算的东西,例如strtotime和其他PHP的日期函数。
另一种可能是直接在SQL中计算。
https://stackoverflow.com/questions/14449263
复制相似问题