私は予約部屋のためのlaravelのプロジェクトを持っています。日付を選択するカレンダーがあります。私は予約の日が夏か冬かをチェックする必要があります。シーズンでない場合は、「シーズン時間ではない」というメッセージを表示します。予約の日付を確認するにはどうすれば複数のシーズンからシーズンの時間に来ることができますか?
私はいくつかのコードを試しました。このコードは、テーブルにエントリが1つしかない場合に機能します。下記の内容を確認して助けてください。ありがとう。
シーズン(表)
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*/
}
}
?>
テーブルに複数のエントリがある場合は、どのような問題がありますか? – Geshode
@Geshode、複数のエントリーがある場合、毎回私は「シーズンタイムではない」を得ています。 –
あなたは明らかにシーズン外です –