views_handler_field_readable_date.inc 3.3 KB

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