You are here

function date_limit_format in Date 6

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

8 calls to date_limit_format()
date_combo_process in date/date_elements.inc
Process an individual date element.
date_formatter_format in date/date.module
Helper function to return the date format used by a specific formatter.
date_formatter_process in date/date.module
Helper function for creating formatted date arrays from a formatter.
date_format_options in date/date_admin.inc
Store personalized format options for each user.
date_popup_input_value in date_popup/date_popup.module
Helper function for extracting a date value out of user input.

... See full list

File

./date_api.module, line 1197
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) {

  // Strip out timezone formatting.
  // Get rid of dash separating date and time if either is missing.
  if (!date_has_time($granularity) || sizeof(array_intersect($granularity, array(
    'year',
    'month',
    'day',
  )) == 0)) {
    $regex[] = '( -)';
  }
  if (!date_has_time($granularity)) {
    $regex[] = '(a|A)';
  }

  // Create regular expressions to remove selected values from string.
  foreach (date_nongranularity($granularity) as $element) {
    switch ($element) {
      case 'year':
        $regex[] = '([\\-/\\.]?[Yy][\\-/\\.,]?)';
        break;
      case 'day':
        $regex[] = '([\\-/\\.]?[lDdj][\\-/\\.,]?)';
        break;
      case 'month':
        $regex[] = '([\\-/\\.]?[FMmn][\\-/\\.,]?)';
        break;
      case 'hour':
        $regex[] = '([HhGg][:]?)';
        break;
      case 'minute':
        $regex[] = '([:]?[i])';
        break;
      case 'second':
        $regex[] = '([:]?[s])';
        break;
      case 'timezone':
        $regex[] = '([OZPe])';
        break;
    }
  }

  // Remove selected values from string.
  // Don't leave any trailing punctuation behind.
  $format = trim(preg_replace($regex, array(), $format));
  return preg_replace('([\\-/\\.,]$)', '', $format);
}