You are here

function date_limit_format in Date 7

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.2 date_api/date_api.module \date_limit_format()

Rewrite a format string so it only includes elements from a specified granularity array.

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

Parameters

$format: a format string

$granularity: an array of allowed date parts, all others will be removed array('year', 'month', 'day', 'hour', 'minute', 'second');

Return value

a format string with all other elements removed

14 calls to date_limit_format()
DateObject::format in date_api/date_api.module
Overrides base format function, formats this date according to its available granularity, unless $force'ed not to limit to granularity.
DateObject::__construct in date_api/date_api.module
Overridden constructor.
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_formatter_process in ./date.module
Helper function for creating formatted date arrays from a formatter.

... See full list

File

date_api/date_api.module, line 1581
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Code

function date_limit_format($format, $granularity) {

  // 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]{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.
  if (!($test = trim(preg_replace('(\\\\\\w{1})', '', $format)))) {
    return '';
  }
  return $format;
}