[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/handlers/ -> views_handler_field_date.inc (source)

   1  <?php
   2  /**
   3   * A handler to provide proper displays for dates.
   4   *
   5   * @ingroup views_field_handlers
   6   */
   7  class views_handler_field_date extends views_handler_field {
   8    function option_definition() {
   9      $options = parent::option_definition();
  10  
  11      $options['date_format'] = array('default' => 'small');
  12      $options['custom_date_format'] = array('default' => '');
  13  
  14      return $options;
  15    }
  16  
  17    function options_form(&$form, &$form_state) {
  18      parent::options_form($form, $form_state);
  19      $time = time();
  20  
  21      $form['date_format'] = array(
  22        '#type' => 'select',
  23        '#title' => t('Date format'),
  24        '#options' => array(
  25          'small' => t('Short date format') . ' ' . format_date($time, 'small'),
  26          'medium' => t('Medium date format') . ' ' . format_date($time, 'medium'),
  27          'large' => t('Long date format') . ' ' . format_date($time, 'large'),
  28          'custom' => t('Custom'),
  29          'raw time ago' => t('Time ago'),
  30          'time ago' => t('Time ago (with "ago" appended)'),
  31          'raw time span' => t('Time span (future dates start with - )'),
  32          'time span' => t('Time span (with "ago/hence" appended)'),
  33        ),
  34        '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
  35      );
  36      $form['custom_date_format'] = array(
  37        '#type' => 'textfield',
  38        '#title' => t('Custom date format'),
  39        '#description' => t('If "Custom", see <a href="http://us.php.net/manual/en/function.date.php" target="_blank">the PHP docs</a> for date formats. If "Time ago", enter the number of different time units to display, which defaults to 2.'),
  40        '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
  41        '#process' => array('views_process_dependency'),
  42        '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span')),
  43      );
  44    }
  45  
  46    function render($values) {
  47      $value = $values->{$this->field_alias};
  48      $format = $this->options['date_format'];
  49      if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span'))) {
  50        $custom_format = $this->options['custom_date_format'];
  51      }
  52      
  53      if ($value) {
  54        $time_diff = time() - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
  55        switch ($format) {
  56          case 'raw time ago':
  57            return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
  58          case 'time ago':
  59            return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
  60          case 'raw time span':
  61            return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
  62          case 'time span':
  63            return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
  64          case 'custom':
  65            if ($custom_format == 'r') {
  66              return format_date($value, $format, $custom_format, null, 'en');
  67            }
  68            return format_date($value, $format, $custom_format);
  69          default:
  70            return format_date($value, $format);
  71        }
  72      }
  73    }
  74  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7