You are here

function rooms_check_dates_validity in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Checks the logical validity of date values.

Parameters

DateTime $start_date: The start date

DateTime $end_date: The end date

bool $today_greater: TRUE in to enforce validating that start_date must be greater than today.

Return value

array An array with error messages.

2 calls to rooms_check_dates_validity()
rooms_booking_manager_results_page in modules/rooms_booking_manager/rooms_booking_manager.module
Constructs the booking results page following an availability search.
rooms_form_start_end_dates_validate in ./rooms.module
Validation callback that could be reused in all the forms that need to validate dates. End date must be greater than start date.

File

./rooms.module, line 1059
Provides basic underlying functionality and configuration options used by all Rooms modules

Code

function rooms_check_dates_validity(DateTime $start_date, DateTime $end_date, $today_greater = FALSE, $single_day_bookings = FALSE) {
  $errors = array();

  // End date must be greater than start date unless single day bookings are
  // enabled, in which case it must not be before the start date.
  if ($end_date < $start_date || $end_date == $start_date && !$single_day_bookings) {
    if ($single_day_bookings) {
      $errors[] = t('End date must be on or after the start date.');
    }
    else {
      $errors[] = t('End date must be after start date.');
    }
  }

  // End date must not be more that the allowed number of days in advance for booking
  $current_date = new DateTime();
  $booking_interval = $current_date
    ->diff($end_date);

  // The following necessary to get a properly instantiated date interval when starting with
  // just seconds
  $d1 = new DateTime();
  $d2 = new DateTime();
  $d2
    ->add(new DateInterval('PT' . variable_get('rooms_advance_bookings_period', 15552000) . 'S'));
  $advanced_bookings_interval = $d2
    ->diff($d1);
  if ($booking_interval
    ->format('%a') > $advanced_bookings_interval
    ->format('%a') && !user_access('book in advance without limitation')) {
    $errors[] = 'You cannot book that far into the future - bookings can only be made ' . $advanced_bookings_interval
      ->format('%a') . ' days in advance';
  }

  // In case the date should be grater than today.
  if ($today_greater) {
    $now = new DateTime();
    $diff1 = $now
      ->setTime(0, 0, 0)
      ->diff($start_date);
    if ($diff1->invert) {
      $errors[] = t('Start date must be current or in the future.');
    }
  }
  return $errors;
}