| Commit | Line | Data |
|---|---|---|
| fe44beb7 | 1 | <?php |
| fe44beb7 EM |
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' => format_date($time, 'small'), | |
| 26 | 'medium' => format_date($time, 'medium'), | |
| 27 | 'large' => format_date($time, 'large'), | |
| 28 | 'custom' => t('Custom'), | |
| 94bdae33 EM |
29 | 'raw time ago' => t('Time ago'), |
| 30 | 'time ago' => t('Time ago (with "ago" appended)'), | |
| 13b9165c EM |
31 | 'raw time span' => t('Time span (future dates start with - )'), |
| 32 | 'time span' => t('Time span (with "ago/hence" appended)'), | |
| fe44beb7 EM |
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" this is the the number of different units to display, which defaults to two.'), | |
| 40 | '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '', | |
| 41 | '#process' => array('views_process_dependency'), | |
| 13b9165c | 42 | '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span')), |
| fe44beb7 EM |
43 | ); |
| 44 | } | |
| 45 | ||
| 46 | function render($values) { | |
| 47 | $value = $values->{$this->field_alias}; | |
| 48 | $format = $this->options['date_format']; | |
| 13b9165c | 49 | if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span'))) { |
| fe44beb7 EM |
50 | $custom_format = $this->options['custom_date_format']; |
| 51 | } | |
| 13b9165c EM |
52 | |
| 53 | if (!$value) { | |
| 54 | return theme('views_nodate'); | |
| 55 | } | |
| 56 | else { | |
| 57 | $time_diff = time() - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence) | |
| 58 | switch ($format) { | |
| 59 | case 'raw time ago': | |
| 60 | return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2); | |
| 61 | case 'time ago': | |
| 62 | return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2))); | |
| 63 | case 'raw time span': | |
| 64 | return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2); | |
| 65 | case 'time span': | |
| 66 | return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2))); | |
| 67 | case 'custom': | |
| 68 | return format_date($value, $format, $custom_format); | |
| 69 | default: | |
| 70 | return format_date($value, $format); | |
| 71 | } | |
| fe44beb7 EM |
72 | } |
| 73 | } | |
| 74 | } |