views_handler_field_readable_date.inc 3.3 KB

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