views_handler_field_date.inc

  1. 3.x handlers/views_handler_field_date.inc
  2. 2.x handlers/views_handler_field_date.inc

File

handlers/views_handler_field_date.inc
View source
  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. $options['date_format'] = array('default' => 'small');
  11. $options['custom_date_format'] = array('default' => '');
  12. return $options;
  13. }
  14. function options_form(&$form, &$form_state) {
  15. parent::options_form($form, $form_state);
  16. $time = time();
  17. $form['date_format'] = array(
  18. '#type' => 'select',
  19. '#title' => t('Date format'),
  20. '#options' => array(
  21. 'small' => t('Short date format') . ' ' . format_date($time, 'small'),
  22. 'medium' => t('Medium date format') . ' ' . format_date($time, 'medium'),
  23. 'large' => t('Long date format') . ' ' . format_date($time, 'large'),
  24. 'custom' => t('Custom'),
  25. 'raw time ago' => t('Time ago'),
  26. 'time ago' => t('Time ago (with "ago" appended)'),
  27. 'raw time span' => t('Time span (future dates start with - )'),
  28. 'time span' => t('Time span (with "ago/hence" appended)'),
  29. ),
  30. '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
  31. );
  32. $form['custom_date_format'] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('Custom date format'),
  35. '#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.'),
  36. '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
  37. '#process' => array('views_process_dependency'),
  38. '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span')),
  39. );
  40. }
  41. function render($values) {
  42. $value = $values->{$this->field_alias};
  43. $format = $this->options['date_format'];
  44. if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span'))) {
  45. $custom_format = $this->options['custom_date_format'];
  46. }
  47. if ($value) {
  48. $time_diff = time() - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
  49. switch ($format) {
  50. case 'raw time ago':
  51. return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
  52. case 'time ago':
  53. return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
  54. case 'raw time span':
  55. return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
  56. case 'time span':
  57. return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
  58. case 'custom':
  59. if ($custom_format == 'r') {
  60. return format_date($value, $format, $custom_format, null, 'en');
  61. }
  62. return format_date($value, $format, $custom_format);
  63. default:
  64. return format_date($value, $format);
  65. }
  66. }
  67. }
  68. }