private static function SmartDateTrait::rangeFormat in Smart Date 3.1.x
Same name and namespace in other branches
- 8.2 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
- 8 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
- 3.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
- 3.0.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
- 3.2.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
- 3.3.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
- 3.4.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
Format a provided range, using provided settings.
Parameters
array $range: The date/time range to format.
array $settings: The date/time range to format.
string $return_type: An option to specify that a string should be returned. If left empty, a render array will be returned instead.
Return value
string|array The formatted range.
1 call to SmartDateTrait::rangeFormat()
- SmartDateTrait::formatSmartDate in src/
SmartDateTrait.php - Creates a formatted date value as a string.
File
- src/
SmartDateTrait.php, line 442
Class
- SmartDateTrait
- Provides friendly methods for smart date range.
Namespace
Drupal\smart_dateCode
private static function rangeFormat(array $range, array $settings, $return_type = '') {
// If a string is requested, return that.
if ($return_type == 'string') {
$pieces = [];
foreach ($range as $key => $parts) {
if ($parts) {
if (!$settings['date_first']) {
// Time should be first so reverse the array.
krsort($parts);
}
$pieces[] = implode($settings['join'], $parts);
}
}
return implode($settings['separator'], $pieces);
}
// Otherwise, return a render array so it can be altered.
foreach ($range as $key => &$parts) {
if ($parts && is_array($parts) && count($parts) > 1) {
$parts['join'] = $settings['join'];
if ($settings['date_first']) {
// Date should be first so sort the array.
ksort($parts);
}
else {
// Time should be first so reverse the array.
krsort($parts);
}
}
elseif (!$parts) {
unset($range[$key]);
}
}
if (count($range) > 1) {
$range['separator'] = $settings['separator'];
krsort($range);
}
// Otherwise, return a nested array.
$output = static::arrayToRender($range);
$output['#attributes']['class'] = [
'smart_date_range',
];
return $output;
}