You are here

function availability_calendar_get_date_format_for_js in Availability Calendars 7.5

Same name and namespace in other branches
  1. 7.3 availability_calendar.inc \availability_calendar_get_date_format_for_js()
  2. 7.4 availability_calendar.inc \availability_calendar_get_date_format_for_js()

Converts a PHP date format to a jQuery date picker format.

Parameters

string $format: The date format to convert. If not given or empty, the (possibly localized) date format for the availability_calendar_date_display date type is converted.

Return value

string

1 call to availability_calendar_get_date_format_for_js()
availability_calendar_add_base_js in ./availability_calendar.inc
Adds the necessary javascript.

File

./availability_calendar.inc, line 116

Code

function availability_calendar_get_date_format_for_js($format = '') {
  $format_conversions = array(
    'j' => 'd',
    'd' => 'dd',
    'z' => 'o',
    'D' => 'D',
    'l' => 'DD',
    'n' => 'm',
    'm' => 'mm',
    'M' => 'M',
    'F' => 'MM',
    'y' => 'y',
    'Y' => 'yy',
    'U' => '@',
    "'" => "''",
  );
  $needs_js_escape = array(
    'o',
    'd',
    'D',
    'm',
    'M',
    "y",
    '@',
    '!',
  );
  if (empty($format)) {
    $format = variable_get('date_format_availability_calendar_date_display', AC_DATE_DISPLAY);
  }
  $js_format = '';
  $is_escaped = FALSE;
  for ($i = 0; $i < strlen($format); $i++) {
    $char = $format[$i];
    if ($is_escaped || !array_key_exists($char, $format_conversions)) {
      if (!$is_escaped && $char === '\\') {

        // Next character is escaped. Skip this \.
        $is_escaped = TRUE;
      }
      else {

        // We are either escaping this character or it is not a format
        // character. In both cases we have to add this character as is, though
        // it may be a character that needs escaping in the js format.
        $js_format .= in_array($char, $needs_js_escape) ? "'{$char}'" : $char;
        $is_escaped = FALSE;
      }
    }
    else {

      // This is a non escaped to be converted character: replace the PHP format
      // with the js format.
      $js_format .= $format_conversions[$char];
    }
  }
  if ($is_escaped) {

    // A \ at the end of the PHP format, add it to the js format as well.
    $js_format .= '\\';
  }
  return $js_format;
}