You are here

function bat_form_input_get_start_end_dates in Booking and Availability Management Tools for Drupal 7

Given a form state array, locate the start/end dates in the input array and instantiate and return DateTime objects.

1 call to bat_form_input_get_start_end_dates()
bat_form_start_end_dates_validate in ./bat.module
Validation callback that can be reused in all forms that need to validate dates.

File

./bat.module, line 596
Provides basic underlying functionality and configuration options used by all BAT modules.

Code

function bat_form_input_get_start_end_dates($form_state) {

  // If form_state['values']['bat_X_date'] is not set, it is an empty array.
  // In this case, we need to check and set values so that
  // _constructor below will not fail.
  if (is_array($form_state['values']['bat_start_date'])) {
    if ($form_state['values']['bat_start_date']['date'] == '') {
      $start = new DateTime();
    }
    else {
      $start = new DateTime($form_state['values']['bat_start_date']['date'] . ' ' . $form_state['values']['bat_start_date']['time']);
    }
  }
  else {
    $start = new DateTime($form_state['values']['bat_start_date']);
  }
  if (is_array($form_state['values']['bat_end_date'])) {
    if ($form_state['values']['bat_end_date']['date'] == '') {
      $end = new DateTime();
    }
    else {
      $end = new DateTime($form_state['values']['bat_end_date']['date'] . ' ' . $form_state['values']['bat_end_date']['time']);
    }
  }
  else {
    $end = new DateTime($form_state['values']['bat_end_date']);
  }
  return array(
    $start,
    $end,
  );
}