You are here

public function view_custom_table_handler_field_date_time::render in Views Custom Table 7

Implements render().

Overrides views_handler_field::render

File

views/handlers/view_custom_table_handler_field_date_time.inc, line 126
Definition of views_handler_field_date.

Class

view_custom_table_handler_field_date_time
A handler to provide proper displays for dates.

Code

public function render($values) {
  $value = $this
    ->get_value($values);
  $value = strtotime($value);
  $format = $this->options['date_format'];
  $available_format = array(
    'custom',
    'raw time ago',
    'time ago',
    'today time ago',
    'raw time hence',
    'time hence',
    'raw time span',
    'time span',
    'raw time span',
    'inverse time span',
    'time span',
  );
  if (in_array($format, $available_format)) {
    $custom_format = $this->options['custom_date_format'];
  }
  if ($value) {
    $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;

    // Will be positive for a datetime in the past (ago),
    // and negative for a datetime in the future (hence).
    $time_diff = REQUEST_TIME - $value;
    switch ($format) {
      case 'raw time ago':
        return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
      case 'time ago':
        return t('%time ago', array(
          '%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2),
        ));
      case 'today time ago':
        $second_format = $this->options['second_date_format'];
        $second_custom_format = $this->options['second_date_format_custom'];
        if (format_date(REQUEST_TIME, 'custom', 'Y-m-d', $timezone) == format_date($value, 'custom', 'Y-m-d', $timezone)) {
          return t('%time ago', array(
            '%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2),
          ));
        }
        elseif ($second_format == 'custom') {
          if ($second_custom_format == 'r') {
            return format_date($value, $second_format, $second_custom_format, $timezone, 'en');
          }
          return format_date($value, $second_format, $second_custom_format, $timezone);
        }
        else {
          return format_date($value, $this->options['second_date_format'], '', $timezone);
        }
      case 'raw time hence':
        return format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2);
      case 'time hence':
        return t('%time hence', array(
          '%time' => format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2),
        ));
      case 'raw time span':
        return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
      case 'inverse time span':
        return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
      case 'time span':
        if ($time_diff < 0) {
          return t('%time hence', array(
            '%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2),
          ));
        }
        else {
          return t('%time ago', array(
            '%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2),
          ));
        }
      case 'custom':
        if ($custom_format == 'r') {
          return format_date($value, $format, $custom_format, $timezone, 'en');
        }
        return format_date($value, $format, $custom_format, $timezone);
      default:
        return format_date($value, $format, '', $timezone);
    }
  }
}