You are here

public static function DateGranularity::limitFormat in Date 8

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

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

Parameters

string $format: A date format string.

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

Return value

string The format string with all other elements removed.

9 calls to DateGranularity::limitFormat()
DateGranularity::partFormat in date_api/lib/Drupal/date_api/DateGranularity.php
Helper function to get a format for a specific part of a date field.
DateSqlHandler::views_formats in date_api/lib/Drupal/date_api/DateSqlHandler.php
@todo.
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.
date_repeat_merge in date_repeat/date_repeat_form.inc
Regroup values back into a consistant array, no matter what state it is in.

... See full list

File

date_api/lib/Drupal/date_api/DateGranularity.php, line 285
Definition of DateGranularity.

Class

DateGranularity
This class manages granularity. It can set granularity, get it from an array, get it from a format string, see if the array has any time or date elements, set and unset various granularity parts, create a nongranularity array of the granularity parts…

Namespace

Drupal\date_api

Code

public static function limitFormat($format, $array) {

  // 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 (!self::hasTime($array) || !self::hasDate($array)) {
    $format = str_replace('\\T', ' ', $format);
    $format = str_replace('T', ' ', $format);
  }
  $regex = array();
  if (!DateGranularity::hasTime($array)) {
    $regex[] = '((?<!\\\\)[a|A])';
  }

  // Create regular expressions to remove selected values from string.
  // Use (?<!\\\\) to keep escaped letters from being removed.
  foreach (self::nongranularity($array) 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})', '', $format));
  if (empty($test)) {
    $format = '';
  }
  return $format;
}