00001 <?php
00007 class views_handler_field_date extends views_handler_field {
00008 function option_definition() {
00009 $options = parent::option_definition();
00010
00011 $options['date_format'] = array('default' => 'small');
00012 $options['custom_date_format'] = array('default' => '');
00013
00014 return $options;
00015 }
00016
00017 function options_form(&$form, &$form_state) {
00018
00019 $date_formats = array();
00020 $date_types = system_get_date_types();
00021 foreach ($date_types as $key => $value) {
00022 $date_formats[$value['type']] = check_plain(t($value['title'] . ' format')) . ': ' . format_date(REQUEST_TIME, $value['type']);
00023 }
00024
00025 $form['date_format'] = array(
00026 '#type' => 'select',
00027 '#title' => t('Date format'),
00028 '#options' => $date_formats + array(
00029 'custom' => t('Custom'),
00030 'raw time ago' => t('Time ago'),
00031 'time ago' => t('Time ago (with "ago" appended)'),
00032 'raw time hence' => t('Time hence'),
00033 'time hence' => t('Time hence (with "hence" appended)'),
00034 'raw time span' => t('Time span (future dates have "-" prepended)'),
00035 'inverse time span' => t('Time span (past dates have "-" prepended)'),
00036 'time span' => t('Time span (with "ago/hence" appended)'),
00037 ),
00038 '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
00039 );
00040 $form['custom_date_format'] = array(
00041 '#type' => 'textfield',
00042 '#title' => t('Custom date format'),
00043 '#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. Otherwise, enter the number of different time units to display, which defaults to 2.'),
00044 '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
00045 '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span')),
00046 );
00047
00048 parent::options_form($form, $form_state);
00049 }
00050
00051 function render($values) {
00052 $value = $this->get_value($values);
00053 $format = $this->options['date_format'];
00054 if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'))) {
00055 $custom_format = $this->options['custom_date_format'];
00056 }
00057
00058 if ($value) {
00059 $time_diff = REQUEST_TIME - $value;
00060 switch ($format) {
00061 case 'raw time ago':
00062 return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
00063 case 'time ago':
00064 return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
00065 case 'raw time hence':
00066 return format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2);
00067 case 'time hence':
00068 return t('%time hence', array('%time' => format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2)));
00069 case 'raw time span':
00070 return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
00071 case 'inverse time span':
00072 return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
00073 case 'time span':
00074 return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
00075 case 'custom':
00076 if ($custom_format == 'r') {
00077 return format_date($value, $format, $custom_format, null, 'en');
00078 }
00079 return format_date($value, $format, $custom_format);
00080 default:
00081 return format_date($value, $format);
00082 }
00083 }
00084 }
00085 }