You are here

function date_limit_format in Date 7.2

Same name and namespace in other branches
  1. 5.2 date_api.module \date_limit_format()
  2. 5 date.inc \date_limit_format()
  3. 6.2 date_api.module \date_limit_format()
  4. 6 date_api.module \date_limit_format()
  5. 7.3 date_api/date_api.module \date_limit_format()
  6. 7 date_api/date_api.module \date_limit_format()

Limits a date format to include only elements from a given granularity array.

Example: date_limit_format('F j, Y - H:i', array('year', 'month', 'day')); returns 'F j, Y'

Parameters

string $format: A date format string.

array $granularity: An array of allowed date parts, all others will be removed.

Return value

string The format string with all other elements removed.

17 calls to date_limit_format()
DateObject::format in date_api/date_api.module
Returns date formatted according to given format.
DateObject::__construct in date_api/date_api.module
Constructs a date object.
date_combo_element_process in ./date_elements.inc
Process an individual date element.
date_formatter_format in ./date.module
Retrieve a date format string from formatter settings.
date_format_date in date_api/date_api.module
Formats a date, using a date type or a custom date format string.

... See full list

File

date_api/date_api.module, line 2344
This module will make the date API available to other modules.

Code

function date_limit_format($format, array $granularity) {

  // Use the advanced drupal_static() pattern to improve performance.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['formats'] =& drupal_static(__FUNCTION__);
  }
  $formats =& $drupal_static_fast['formats'];
  $format_granularity_cid = $format . '|' . implode(',', $granularity);
  if (isset($formats[$format_granularity_cid])) {
    return $formats[$format_granularity_cid];
  }

  // If punctuation has been escaped, remove the escaping. Done using strtr()
  // because it is easier than getting the escape character extracted using
  // preg_replace().
  $replace = array(
    '\\-' => '-',
    '\\:' => ':',
    "\\'" => "'",
    '\\. ' => ' . ',
    '\\,' => ',',
  );
  $format = strtr($format, $replace);

  // Get the 'T' out of ISO date formats that don't have both date and time.
  if (!date_has_time($granularity) || !date_has_date($granularity)) {
    $format = str_replace('\\T', ' ', $format);
    $format = str_replace('T', ' ', $format);
  }
  $regex = array();
  if (!date_has_time($granularity)) {
    $regex[] = '((?<!\\\\)[a|A])';
  }

  // Create regular expressions to remove selected values from string.
  // Use (?<!\\\\) to keep escaped letters from being removed.
  foreach (date_nongranularity($granularity) as $element) {
    switch ($element) {
      case 'year':
        $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[Yy])';
        break;
      case 'day':
        $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[l|D|d|dS|j|jS|N|w|W|z]{1,2})';
        break;
      case 'month':
        $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[FMmn])';
        break;
      case 'hour':
        $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[HhGg])';
        break;
      case 'minute':
        $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[i])';
        break;
      case 'second':
        $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[s])';
        break;
      case 'timezone':
        $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[TOZPe])';
        break;
    }
  }

  // Remove empty parentheses, brackets, pipes.
  $regex[] = '(\\(\\))';
  $regex[] = '(\\[\\])';
  $regex[] = '(\\|\\|)';

  // Remove selected values from string.
  $format = trim(preg_replace($regex, array(), $format));

  // Remove orphaned punctuation at the beginning of the string.
  $format = preg_replace('`^([\\-/\\.,:\'])`', '', $format);

  // Remove orphaned punctuation at the end of the string.
  $format = preg_replace('([\\-/,:\']$)', '', $format);
  $format = preg_replace('(\\$)', '', $format);

  // Trim any whitespace from the result.
  $format = trim($format);

  // After removing the non-desired parts of the format, test if the only things
  // left are escaped, non-date, characters. If so, return nothing.
  // Using S instead of w to pick up non-ASCII characters.
  $test = trim(preg_replace('(\\\\\\S{1,3})u', '', $format));
  if (empty($test)) {
    $format = '';
  }

  // Store the return value in the static array for performance.
  $formats[$format_granularity_cid] = $format;
  return $format;
}