function date_limit_format in Date 6.2
Same name and namespace in other branches
- 5.2 date_api.module \date_limit_format()
- 5 date.inc \date_limit_format()
- 6 date_api.module \date_limit_format()
- 7.3 date_api/date_api.module \date_limit_format()
- 7 date_api/date_api.module \date_limit_format()
- 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()
- date_api_filter_handler::date_parts_form in includes/
date_api_filter_handler.inc - A form element to select date part values.
- 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_popup/
date_popup.module - Store personalized format options for each user.
File
- ./
date_api.module, line 1639 - 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|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;
}