You are here

function date_limit_format in Date 5

Same name and namespace in other branches
  1. 5.2 date_api.module \date_limit_format()
  2. 6.2 date_api.module \date_limit_format()
  3. 6 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, turn 'F j, Y - H:i' into 'F j, Y'

Parameters

$format: a format string

$granularity: an array of allowed date parts, all others will be removed array('Y', 'M', 'D', 'H', 'N', 'S');

Return value

a format string with all other elements removed

2 calls to date_limit_format()
date_formatter_setup_form in ./date_admin.inc
A form to create a date formatter option
date_ical_parse_duration in ./date_api_ical.inc
Parse the duration of the event.

File

./date.inc, line 2307
Date/time API functions

Code

function date_limit_format($format, $granularity) {
  $regex = array();

  // Get rid of dash separating date and time if either is missing.
  if (!date_has_time($granularity) || sizeof(array_intersect($granularity, array(
    'Y',
    'M',
    'D',
  )) == 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 'Y':
        $regex[] = '([\\-/\\.]?[Yy][\\-/\\.,]?)';
        break;
      case 'D':
        $regex[] = '([\\-/\\.]?[lDdj][\\-/\\.,]?)';
        break;
      case 'M':
        $regex[] = '([\\-/\\.]?[FMmn][\\-/\\.,]?)';
        break;
      case 'H':
        $regex[] = '([HhGg][:]?)';
        break;
      case 'N':
        $regex[] = '([:]?[i])';
        break;
      case 'S':
        $regex[] = '([:]?[s])';
        break;
    }
  }

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