Php MySQL查找两个日期是否在多行的日期范围内
我正在使用Codeigniter+MySQL+Active Record,我正在构建一个项目,其中包含房价的时间段 我的mysql表中有下面的模式 包期 房间\套餐\价格 正如您在示例Php MySQL查找两个日期是否在多行的日期范围内,php,mysql,sql,codeigniter,date,Php,Mysql,Sql,Codeigniter,Date,我正在使用Codeigniter+MySQL+Active Record,我正在构建一个项目,其中包含房价的时间段 我的mysql表中有下面的模式 包期 房间\套餐\价格 正如您在示例01-08-17至08-08-2017,10-08-2017至17-08-2017中所看到的那样, 对于给定的搜索周期(假设2017年8月02日至2017年8月14日),我如何找到不仅超过搜索周期而且在搜索周期之外没有1天的包id 我的意思是,package\u idperiods(从多个mysql行到多个my
01-08-17至08-08-2017
,10-08-2017至17-08-2017
中所看到的那样,
对于给定的搜索周期(假设2017年8月02日至2017年8月14日
),我如何找到不仅超过搜索周期而且在搜索周期之外没有1天的包id
我的意思是,package\u id
periods(从多个mysql行到多个mysql行)应该覆盖搜索期间的所有天,而不留下任何间隔
public function getFHotels($checkin = null, $checkout = null, $adults = null, $packageType = null, $package_id = null, $limit, $start, $lang_id) {
$this->db->select('DISTINCT(hotels.hotel_id)')
->from('hotels')
->join('rooms', 'rooms.hotel_id=hotels.hotel_id')
->where('rooms.room_active', 1)
->where('hotels.hotel_active', 1)
->limit($limit, $start);
if ($packageType) {
$this->db->where('rooms.room_package_id', $packageType);
}
if ($package_id && $adults) {
//if $package_id is given, do not search for checkin-checkout
$this->db->join('room_package_prices', 'room_package_prices.room_id=rooms.room_id');
$this->db->where('room_package_prices.package_period_id', $package_id);
$this->db->where('room_package_prices.adults', $adults);
$this->db->where('room_package_prices.price>', 0);
} elseif ($checkin && $checkout && $adults) {
//if $checkin and $checkout is given, search for the period_from and period_to
//Here goes my actual question
}
$qry = $this->db->get();
if ($qry->num_rows() > 0)
return $qry->result_array();
return FALSE;
}
好的,所以我用另一种方式自己解决了这个问题。我想在我的model functiongetFHotels
中实现这个功能,但找不到实现的方法
因此,我在控制器中分离了这个功能,并为此创建了一个新的模型函数。长话短说,这个想法是
从客户端获取所需的搜索周期(签入-签出)
循环检查从签入到签出的每一天,并查询表中的每一天,该表包含哪一天的包id
在数组中推送每日结果
找到数组的交点。如果数组的每个键中都有一个package_id,那么我已经找到了它
代码如下:
$begin = new DateTime($checkin);
$end = new DateTime($checkout);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
$legit_packages = array();
//loop through each day and ask the table if a package_id includes this day
foreach ($period as $dt) {
//add each day results in legit_day_packages array
$legit_day_packages = array();
$day = $dt->format("Y-m-d");
//find the packages that include the day
$periods_found = $this->hotel_model->findFPackagePeriodsPerDay($day);
if ($periods_found) {
//foreach found packages, array push in legit_day_packages the results
foreach ($periods_found as $p_f_key => $p_f) {
array_push($legit_day_packages, $p_f['package_id']);
}
}
//Push the legit_day_packages into $legit_packages array. This array will have 1 key per day, containing the package_ids, if found
array_push($legit_packages, $legit_day_packages);
}
//Find the intersect package_id. In this was i exclude the 'gaps'. A package_id must appear in every key per day.
$first = $legit_packages[0];
for ($i = 1; $i < count($legit_packages); $i++) {
$result = array_intersect($first, $legit_packages[$i]);
$first = $result;
}
//the $result contains all the package_id that satisfies both the range and no 'gap' in the period.
var_dump($result);
$begin=newdatetime($checkin);
$end=新日期时间($checkout);
$interval=DateInterval::createFromDateString('1天');
$period=newdateperiod($begin,$interval,$end);
$legit_packages=array();
//循环每一天,询问表中是否包含这一天的包裹id
foreach(期间为$dt){
//将每天的结果添加到合法的\u day\u包数组中
$legit_day_packages=array();
$day=$dt->格式(“Y-m-d”);
//查找包含当天的套餐
$periods\u found=$this->hotel\u model->findFPackagePeriodsPerDay($day);
如果($u找到){
//对于每个找到的包,数组推入合法的\u day \u包结果
foreach($p\f\u键=>$p\f){
数组推送($legit_day_packages,$p_f['package_id');
}
}
//将legit_day_程序包推送到$legit_程序包数组中。此数组每天将有1个键,如果找到,则包含程序包ID
阵列推送($legit\u套餐,$legit\u day\u套餐);
}
//查找intersect包的id。在这里我排除了“间隙”。每天每个密钥中都必须出现一个包id。
$first=$legit_包[0];
对于($i=1;$i
模型函数
公共函数findFPackagePeriodsPerDay($day){
$qry=$this->db->select('packages.package\u id')
->from('包期')
->join('packages','packages.packages\u id=packages\u periods.packages\u id'))
->其中('packages.is\u package\u type',1)
->其中('package_periods.period_from=',$day)
->get();
如果($qry->num\u rows()>0)
返回$qry->result_array();
返回FALSE;
}
您尝试过什么?您还没有与我们共享任何代码您的查询在哪里?Hmmmm还没有尝试任何操作,因为我甚至不知道如何查询它,但让我在几分钟后编辑以添加一个简单的模型函数:)您可能会发现这很有用,添加了必须参与查询的2个表和我已经编写的函数以及一些注释来帮助您。
public function findFPackagePeriodsPerDay($day) {
$qry = $this->db->select('packages.package_id')
->from('package_periods')
->join('packages', 'packages.package_id=package_periods.package_id')
->where('packages.is_package_type', 1)
->where('package_periods.period_from<=', $day)
->where('package_periods.period_to>=', $day)
->get();
if ($qry->num_rows() > 0)
return $qry->result_array();
return FALSE;
}