I'm building reservation web app. I have array pulled from service, contains blocked dates. using calendar widget I want to make reservation during 2014-07-30 until 2014-08-04. since my reservation date is found in blocked dates (2nd index [2014-08-01 - 2014-08-02] is blocked then should return false (cannot continue reservation).
$checkin = '2014-07-30';
$checkout = '2014-08-04';
$blockedDates = array(
array(
'start' => '2014-07-24',
'end' => '2014-07-27'
),
array(
'start' => '2014-08-01',
'end' => '2014-08-02'
),
array(
'start' => '2014-08-12',
'end' => '2014-08-15'
)
);
function morfora($blokedDates, $checkin, $checkout)
{
foreach ($blokedDates as $date) {
if (($checkin >= $date['start'] && $checkin <= $date['end']) || ($checkout <= $date['start'] && $checkout >= $date['end'])) {
return false;
}
}
return true;
}
var_dump(morfora($blokedDates, $checkin, $checkout)); //still true; should be false;
I need your help guys, thanks before.
you have a mistake in "($checkin >= $date['start'] && $checkin <= $date['end']) || ($checkout <= $date['start'] && $checkout >= $date['end'])" The conditional statement never formed,so always return true
try this :
function checkBookedDate($start_date, $end_date, $date_range)
{
/**
* @var $start_object DateTime
*/
$start_object = DateTime::createFromFormat('Y-m-d', $start_date);
$end_object = DateTime::createFromFormat('Y-m-d', $end_date);
if (is_array($date_range)) {
foreach ($date_range as $row) {
$range_start = DateTime::createFromFormat('Y-m-d', $row['start']);
$range_end = DateTime::createFromFormat('Y-m-d', $row['end']);
if (($start_object > $range_start && $start_object < $range_end) ||
($end_object > $range_start && $end_object < $range_end)) {
return true;
}
}
return false;
} else {
return false;
}
}