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