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 hence' => t('Time hence'), |
---|
32 | 'time hence' => t('Time hence (with "hence" appended)'), |
---|
33 | 'raw time span' => t('Time span (future dates start with - )'), |
---|
34 | 'inverse time span' => t('Time span (past dates start with - )'), |
---|
35 | 'time span' => t('Time span (with "ago/hence" appended)'), |
---|
36 | ), |
---|
37 | '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small', |
---|
38 | ); |
---|
39 | $form['custom_date_format'] = array( |
---|
40 | '#type' => 'textfield', |
---|
41 | '#title' => t('Custom date format'), |
---|
42 | '#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.'), |
---|
43 | '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '', |
---|
44 | '#process' => array('views_process_dependency'), |
---|
45 | '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'inverse time span', 'time span')), |
---|
46 | ); |
---|
47 | } |
---|
48 | |
---|
49 | function render($values) { |
---|
50 | $value = $this->get_value($values); |
---|
51 | $format = $this->options['date_format']; |
---|
52 | if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'inverse time span', 'time span'))) { |
---|
53 | $custom_format = $this->options['custom_date_format']; |
---|
54 | } |
---|
55 | |
---|
56 | if ($value) { |
---|
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 hence': |
---|
64 | return format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2); |
---|
65 | case 'time hence': |
---|
66 | return t('%time hence', array('%time' => format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2))); |
---|
67 | case 'raw time span': |
---|
68 | return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2); |
---|
69 | case 'inverse time span': |
---|
70 | return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2); |
---|
71 | case 'time span': |
---|
72 | return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2))); |
---|
73 | case 'custom': |
---|
74 | if ($custom_format == 'r') { |
---|
75 | return format_date($value, $format, $custom_format, null, 'en'); |
---|
76 | } |
---|
77 | return format_date($value, $format, $custom_format); |
---|
78 | default: |
---|
79 | return format_date($value, $format); |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|