php - 遍历日期并获取两个日期之间的最小日差

标签 php mysql datetime web-applications

您好,这是我今年第一次提问。 请帮助我,我求求你。

我有这个预订表,其中包含开始日期和结束日期,现在我想遍历它们并记下它们与我当前预订日期的不同间隔。在我获得所有不同的时间间隔后,我还想遍历该时间间隔并获得可能的或最近的一天。

最近的一天是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

我的问题是如何正确使用这些代码,尤其是在获取数据库中的数据时。 如何改进在同一个房间中搜索所有其他结束日期和开始日期的搜索。

最佳答案

如果您使用 MySQL DateTime 字段,它应该能够将从数据库获取的字符串转换为您可以计算的内容,例如strtotime 和 PHP 的其他日期函数。

另一种可能性是直接在 SQL 中计算。

关于php - 遍历日期并获取两个日期之间的最小日差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14449263/

相关文章:

php - 尝试测试 UPS "Quantum"接口(interface) - XML 文档格式不正确

javascript - HTML 表单的日期/时间输入

javascript - highchart 通过在下拉列表中选择月份来更改图表

sql - 如何在sql中写这个查询

r - 如何使用R创建时间螺旋图

MYSQL Where on DateTime 错误结果

php - CodeIgniter 没有加载扩展的 MY_Exceptions

PHPList 对于带有前导、尾随或多个相邻点的电子邮件地址失败

用于检查服务器上是否允许 .htaccess 的 php 脚本(函数)

mysql - 从 300 万行中随机选择