You are here

date_range_formatter.module in Date range formatter 6

File

date_range_formatter.module
View source
<?php

/**
 * Implements hook_field_formatter_info().
 */
function date_range_formatter_field_formatter_info() {
  $formatters = array(
    'date_range_without_time' => array(
      'label' => t('Date range (without time)'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
    ),
  );
  return $formatters;
}
function date_range_formatter_theme() {
  return array(
    'date_range_formatter_formatter_date_range_without_time' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
}
function theme_date_range_formatter_formatter_date_range_without_time($element) {
  $formatter = $element['#formatter'];
  switch ($formatter) {
    case 'date_range_without_time':
      $item = $element['#item'];
      $date_item = date_formatter_process($element);
      if (empty($item['value2']) || $item['value'] == $item['value2']) {
        $format = 'd F Y';
        $date = date_formatter_process($element);
        $date_str = format_date(strtotime($date_item['value']['db']['datetime']), 'custom', t($format));
        $element = '<span class="date-display-single">' . filter_xss_admin($date_str) . '</span>';
      }
      else {
        $date1 = strtotime($date_item['value']['db']['datetime']);
        $date2 = strtotime($date_item['value2']['db']['datetime']);
        $format = 'd F Y - {d} {F} {Y}';
        if (date('Y', $date1) === date('Y', $date2)) {
          $format = 'd F - {d} {F} Y';
        }
        if (date('m.Y', $date1) === date('m.Y', $date2)) {
          $format = 'd - {d} F Y';
        }
        if (date('d.m.Y', $date1) === date('d.m.Y', $date2)) {
          $format = 'd F Y';
        }
        $date_str = format_date($date1, 'custom', preg_replace('/\\{([a-zA-Z])\\}/', '{\\\\$1}', t($format)));
        $matches = array();
        if (preg_match_all('/\\{([a-zA-Z])\\}/', $date_str, $matches)) {
          foreach ($matches[1] as $match) {
            $date_str = preg_replace('/\\{' . $match . '\\}/', format_date($date2, 'custom', $match), $date_str);
          }
        }
        $element = '<div class="date-display-range">' . filter_xss_admin($date_str) . '</div>';
      }
      break;
  }
  return $element;
}