views_handler_field_date.inc

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

Definition of views_handler_field_date.

File

handlers/views_handler_field_date.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_date.
  5. */
  6. /**
  7. * A handler to provide proper displays for dates.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_date extends views_handler_field {
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['date_format'] = array('default' => 'small');
  15. $options['custom_date_format'] = array('default' => '');
  16. $options['second_date_format_custom'] = array('default' => '');
  17. $options['second_date_format'] = array('default' => 'small');
  18. $options['timezone'] = array('default' => '');
  19. return $options;
  20. }
  21. function options_form(&$form, &$form_state) {
  22. $date_formats = array();
  23. $date_types = system_get_date_types();
  24. foreach ($date_types as $key => $value) {
  25. $date_formats[$value['type']] = t('@date_format format', array('@date_format' => $value['title'])) . ': ' . format_date(REQUEST_TIME, $value['type']);
  26. }
  27. $form['date_format'] = array(
  28. '#type' => 'select',
  29. '#title' => t('Date format'),
  30. '#options' => $date_formats + array(
  31. 'custom' => t('Custom'),
  32. 'raw time ago' => t('Time ago'),
  33. 'time ago' => t('Time ago (with "ago" appended)'),
  34. 'today time ago' => t('Time ago (with "ago" appended) for today\'s date, but not for other dates'),
  35. 'raw time hence' => t('Time hence'),
  36. 'time hence' => t('Time hence (with "hence" appended)'),
  37. 'raw time span' => t('Time span (future dates have "-" prepended)'),
  38. 'inverse time span' => t('Time span (past dates have "-" prepended)'),
  39. 'time span' => t('Time span (with "ago/hence" appended)'),
  40. ),
  41. '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
  42. );
  43. $form['custom_date_format'] = array(
  44. '#type' => 'textfield',
  45. '#title' => t('Custom date format'),
  46. '#description' => t('If "Custom", see the <a href="@url" target="_blank">PHP manual</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.', array('@url' => 'http://php.net/manual/function.date.php')),
  47. '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
  48. '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'today time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span')),
  49. );
  50. $form['second_date_format'] = array(
  51. '#type' => 'select',
  52. '#title' => t('Second date format'),
  53. '#options' => $date_formats + array(
  54. 'custom' => t('Custom'),
  55. ),
  56. '#description' => t('The date format which will be used for rendering dates other than today.'),
  57. '#default_value' => isset($this->options['second_date_format']) ? $this->options['second_date_format'] : 'small',
  58. '#dependency' => array('edit-options-date-format' => array('today time ago')),
  59. );
  60. $form['second_date_format_custom'] = array(
  61. '#type' => 'textfield',
  62. '#title' => t('Custom date format of second date'),
  63. '#description' => t('If "Custom" is selected in "Second date format", see the <a href="@url" target="_blank">PHP manual</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.', array('@url' => 'http://php.net/manual/function.date.php')),
  64. '#default_value' => isset($this->options['second_date_format_custom']) ? $this->options['second_date_format_custom'] : '',
  65. // We have to use states instead of ctools dependency because dependency
  66. // doesn't handle multiple conditions.
  67. '#states' => array(
  68. 'visible' => array(
  69. '#edit-options-date-format' => array('value' => 'today time ago'),
  70. '#edit-options-second-date-format' => array('value' => 'custom'),
  71. ),
  72. ),
  73. // We have to use ctools dependency too because states doesn't add the
  74. // correct left margin to the element's wrapper.
  75. '#dependency' => array(
  76. // This condition is handled by form API's states.
  77. // 'edit-options-date-format' => array('today time ago'),
  78. 'edit-options-second-date-format' => array('custom'),
  79. ),
  80. );
  81. $form['timezone'] = array(
  82. '#type' => 'select',
  83. '#title' => t('Timezone'),
  84. '#description' => t('Timezone to be used for date output.'),
  85. '#options' => array('' => t('- Default site/user timezone -')) + system_time_zones(FALSE),
  86. '#default_value' => $this->options['timezone'],
  87. '#dependency' => array('edit-options-date-format' => array_merge(array('custom'), array_keys($date_formats))),
  88. );
  89. parent::options_form($form, $form_state);
  90. }
  91. function render($values) {
  92. $value = $this->get_value($values);
  93. $format = $this->options['date_format'];
  94. if (in_array($format, array('custom', 'raw time ago', 'time ago', 'today time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'))) {
  95. $custom_format = $this->options['custom_date_format'];
  96. }
  97. if ($value) {
  98. $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
  99. $time_diff = REQUEST_TIME - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
  100. switch ($format) {
  101. case 'raw time ago':
  102. return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
  103. case 'time ago':
  104. return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
  105. case 'today time ago':
  106. $second_format = $this->options['second_date_format'];
  107. $second_custom_format = $this->options['second_date_format_custom'];
  108. if (format_date(REQUEST_TIME, 'custom', 'Y-m-d', $timezone) == format_date($value, 'custom', 'Y-m-d', $timezone)) {
  109. return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
  110. }
  111. elseif ($second_format == 'custom') {
  112. if ($second_custom_format == 'r') {
  113. return format_date($value, $second_format, $second_custom_format, $timezone, 'en');
  114. }
  115. return format_date($value, $second_format, $second_custom_format, $timezone);
  116. }
  117. else {
  118. return format_date($value, $this->options['second_date_format'], '', $timezone);
  119. }
  120. case 'raw time hence':
  121. return format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2);
  122. case 'time hence':
  123. return t('%time hence', array('%time' => format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2)));
  124. case 'raw time span':
  125. return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
  126. case 'inverse time span':
  127. return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
  128. case 'time span':
  129. return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
  130. case 'custom':
  131. if ($custom_format == 'r') {
  132. return format_date($value, $format, $custom_format, $timezone, 'en');
  133. }
  134. return format_date($value, $format, $custom_format, $timezone);
  135. default:
  136. return format_date($value, $format, '', $timezone);
  137. }
  138. }
  139. }
  140. }