我在laravel项目预订房间。我有一个日历来选择日期。我需要查看夏季或冬季的预订日期。如果不是季节性的话,请显示一条消息"抱歉不是季节时间"
我尝试了一些代码。如果表中只有一个条目,则此代码将起作用。请查看下面给出的详细信息并帮助我。感谢。
季节(表格)
id summer summer_open summer_close winter winter_open winter_close hotel
1 2018 2018-05-01 2018-09-20 2017 2017-11-01 2018-04-20 1
2 2019 2019-05-01 2019-09-20 2018 2018-11-01 2019-04-20 1
3 2018 2018-05-01 2018-09-20 2017 2017-11-01 2018-04-20 2
4 2019 2019-05-01 2019-09-20 2018 2018-11-01 2019-04-20 2
代码
<?php
protected function generateDates($now, $end){
$period = new DatePeriod(
new DateTime($now),
new DateInterval('P1D'),
new DateTime($end)
);
return $period;
}
public function checkAvailability(Request $request){
$dateBegin = $request->dateFrom //2019-09-17;
$dateEnd = $request->dateTo //2019-09-22;
/* generating dates b/w two dates */
$generateBookingDates = $this->generateDates($dateBegin, $dateEnd);
/* selecting seasons */
$seasons = Season::where('hotel', $request->hotelId)->get();
foreach ($generateBookingDates as $key => $generateBookingDate) {
$generateBookingDat = $generateBookingDate->format('Y-m-d'); //2019-09-17, 2019-09-18, 2019-09-19, 2019-09-20, 2019-09-21
/* Checking season begin */
foreach($seasons as $season) {
if(($generateBookingDat >= ($season->summer_open)->format('Y-m-d')) && ($generateBookingDat < ($season->summer_close)->format('Y-m-d')))
{
print_r($generateBookingDat. ' booked on summer season ');
}
elseif(($generateBookingDat >= ($season->winter_open)->format('Y-m-d')) && ($generateBookingDat < ($season->winter_close)->format('Y-m-d')))
{
print_r($generateBookingDat. ' booked on winter season ');
}
else {
print_r($generateBookingDat. ' Sorry not a season time ');
}
}
/* Checking season end*/
}
}
?>
答案 0 :(得分:0)
由于您有多个季节,取决于年份,您必须在预订日期有效时打破循环:
foreach ($generateBookingDates as $key => $generateBookingDate) {
$generateBookingDat = $generateBookingDate->format('Y-m-d'); //2019-09-17, 2019-09-18, 2019-09-19, 2019-09-20, 2019-09-21
$bookingDateSeason = null;
$bookingDateSeasonType = null;
/* Checking season begin */
foreach($seasons as $season) {
if(($generateBookingDat >= ($season->summer_open)->format('Y-m-d')) && ($generateBookingDat < ($season->summer_close)->format('Y-m-d')))
{
$bookingDateSeasonType = 'summer';
$bookingDateSeason = $season;
/* Stop the loop here at the first valid season */
break;
}
elseif(($generateBookingDat >= ($season->winter_open)->format('Y-m-d')) && ($generateBookingDat < ($season->winter_close)->format('Y-m-d')))
{
$bookingDateSeasonType = 'winter';
$bookingDateSeason = $season;
/* Stop the loop here at the first valid season */
break;
}
}
if ($bookingDateSeason && $bookingDateSeasonType)
{
print_r($generateBookingDat . ' booked on ' . $bookingDateSeasonType . ' season ' . $bookingDateSeason->{$bookingDateSeasonType});
}
else
{
print_r($generateBookingDat . ' Sorry not a season time ');
}
}
// 2019-09-17 booked on summer season 2019
// 2019-09-18 booked on summer season 2019
// 2019-09-19 booked on summer season 2019
// 2019-09-20 Sorry not a season time
// 2019-09-21 Sorry not a season time
此代码应该可以解决问题,但在我看来,您应该首先根据预订日期的年份来检索季节。通过这种方式,您可以避免在多个季节中循环。
/* generating dates b/w two dates */
$generateBookingDates = $this->generateDates($dateBegin, $dateEnd);
/* selecting seasons */
//$seasons = Season::where('hotel', $request->hotelId)->get(); <--- Don't need this line anymore
foreach ($generateBookingDates as $key => $generateBookingDate) {
$generateBookingDat = $generateBookingDate->format('Y-m-d'); //2019-09-17, 2019-09-18, 2019-09-19, 2019-09-20, 2019-09-21
$bookingYear = $generateBookingDate->format('Y');
$season = /* Season::where('hotel', $request->hotelId)->get(); ... and where year = $bookingYear */
if(($generateBookingDat >= ($season->summer_open)->format('Y-m-d')) && ($generateBookingDat < ($season->summer_close)->format('Y-m-d')))
{
print_r($generateBookingDat. ' booked on summer season ');
}
elseif(($generateBookingDat >= ($season->winter_open)->format('Y-m-d')) && ($generateBookingDat < ($season->winter_close)->format('Y-m-d')))
{
print_r($generateBookingDat. ' booked on winter season ');
}
else
{
print_r($generateBookingDat. ' Sorry not a season time ');
}
}
// 2019-09-17 booked on summer season
// 2019-09-18 booked on summer season
// 2019-09-19 booked on summer season
// 2019-09-20 Sorry not a season time
// 2019-09-21 Sorry not a season time
答案 1 :(得分:0)
我的建议是你需要改进表结构然后你将处理这些类型的情况。你的表看起来像这样: -
id season_type open_date close_date hotel year
1 winter 2018-05-01 2018-09-20 1 2018
2 summer 2019-05-01 2019-09-20 1 2019
3 summer 2018-05-01 2018-09-20 2 2018
4 summer 2019-05-01 2019-09-20 2 2019
if you want to save year you can save
完成相同的功能后,您可以: -
$seasons = Season::where('hotel',1)->get();
Example 1:-
$dateBegin = 2019-09-17;
$dateEnd = 2019-09-22;
Example 2:-
$dateBegin = 2019-09-17;
$dateEnd = 2019-09-19;
foreach($seasons as $key=> $season){
if($season->open_date >= $dateBegin && $season->close_date <= $dateEnd){
print_r('booked on .'$season->season_type'. season '); //save this in one array variable
}else{
print_r(Sorry not a season time');
}
}
如果您真的使用了表结构,将来会遇到很多问题。希望它有所帮助