Tripal v1.0 (6.x-1.0)
|
00001 <?php 00002 00010 class views_handler_field_readable_date extends views_handler_field { 00011 function option_definition() { 00012 $options = parent::option_definition(); 00013 00014 $options['date_format'] = array('default' => 'small'); 00015 $options['custom_date_format'] = array('default' => ''); 00016 00017 return $options; 00018 } 00019 00020 function options_form(&$form, &$form_state) { 00021 parent::options_form($form, $form_state); 00022 $time = time(); 00023 00024 $form['date_format'] = array( 00025 '#type' => 'select', 00026 '#title' => t('Date format'), 00027 '#options' => array( 00028 'small' => format_date($time, 'small'), 00029 'medium' => format_date($time, 'medium'), 00030 'large' => format_date($time, 'large'), 00031 'custom' => t('Custom'), 00032 'raw time ago' => t('Time ago'), 00033 'time ago' => t('Time ago (with "ago" appended)'), 00034 'raw time span' => t('Time span (future dates start with - )'), 00035 'time span' => t('Time span (with "ago/hence" appended)'), 00036 ), 00037 '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small', 00038 ); 00039 $form['custom_date_format'] = array( 00040 '#type' => 'textfield', 00041 '#title' => t('Custom date format'), 00042 '#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" this is the the number of different units to display, which defaults to two.'), 00043 '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '', 00044 '#process' => array('views_process_dependency'), 00045 '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span')), 00046 ); 00047 } 00048 00049 function render($values) { 00050 $value = $values->{$this->field_alias}; 00051 00052 // value is currently a CCYY:MM:DD HH:MM:SS format 00053 // change it to unix timestamp so rest works 00054 $value = strtotime($value); 00055 00056 $format = $this->options['date_format']; 00057 if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span'))) { 00058 $custom_format = $this->options['custom_date_format']; 00059 } 00060 00061 if (!$value) { 00062 return theme('views_nodate'); 00063 } 00064 else { 00065 $time_diff = time() - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence) 00066 switch ($format) { 00067 case 'raw time ago': 00068 return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2); 00069 case 'time ago': 00070 return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2))); 00071 case 'raw 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 return format_date($value, $format, $custom_format); 00077 default: 00078 return format_date($value, $format); 00079 } 00080 } 00081 } 00082 }