0

I have to add a condition to my date format so that it doesn't display :00 when the minutes are at 00.

For example (with current format)

Tuesday, January 24, 2017 - 9:15 am
Tuesday, January 24, 2017 - 9:00 am

should display (what I'm looking for)

Tuesday, January 24, 2017 - 9:15 am
Tuesday, January 24, 2017 - 9 am

Thank you!

Eric B.
  • 338
  • 3
  • 14
  • Possible duplicate of [How can a custom Drupal date format be added?](http://stackoverflow.com/questions/8375674/how-can-a-custom-drupal-date-format-be-added) – Fky Jan 27 '17 at 13:39
  • Thanks, but no. You can't dynamically customize the output based on the value of the input using this method. – Eric B. Jan 27 '17 at 13:50

1 Answers1

0

I've implemented the solution using the hook_field_attach_preprocess_alter hook from the field.api.php

We already had a module called "local" in our application that I could add this code to.

/**
 * Implements hook_field_attach_preprocess_alter
 */
function local_field_attach_preprocess_alter(&$variables, $context) {  
  global $language;

  foreach ($variables['content'] as $field_name => $field) {

    // remove minutes formating if at 00 on field type datestamp.
    if (isset($field['#field_type']) && $field['#field_type'] === 'datestamp') {
      $tmp_date = $variables['content'][$field_name][0]['#markup'];

      switch ($language->language) {
        case 'en':
          $variables['content'][$field_name][0]['#markup'] = str_replace(':00', '', $tmp_date);
          break;

        case 'fr':
          $variables['content'][$field_name][0]['#markup'] = str_replace('h 00', 'h', $tmp_date);
          break;

        default:
          break;
      }
    }
  }
}
Eric B.
  • 338
  • 3
  • 14