How to create a dynamic date format? I'm fairly new to Drupal and I'm struggling to find where and how I should approach the problem I'm describing below. I suspect I will have to dive in the code but that's ok I'm comfortable with PHP.

I have a content type "Event" with a "Date &amp; Time" field called "Event Start Date". I've configured the Date format to be "l, F d, Y - h:i a", but it's not exactly what I'm looking for.

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

For example

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


should display

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


How and where should I be doing this in code?

Preferably, I would like to be able to tell when to apply this rule.
I think the best solution is used custom field formatter for date type:

/**
 * Implements hook_field_formatter_info().
 */
function mymodule_field_formatter_info() {
  return array(
      'mymodule_custom_datestamp' =&gt; array(
          'label' =&gt; t('Custom datestamp formatter'),
          'field types' =&gt; array('datestamp'), 
      ),
      'mymodule_custom_date' =&gt; array(
          'label' =&gt; t('Custom date formatter'),
          'field types' =&gt; array('date', 'datetime'), 
      ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function mymodule_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  switch ($display['type']) {
    case 'mymodule_custom_datestamp':
      foreach ($items as $delta =&gt; $item) {
        $tmp = (integer)$item['value'];
        $minutes = (integer)date('i', $tmp);
        if(empty($minutes)){ //you'll update only values with :00
          $element[$delta] = array('#markup' =&gt; date('l, F d, Y - h a', $tmp));
        }else{
          $element[$delta] = array('#markup' =&gt; date('l, F d, Y - h:i a', $tmp));
        }
      }
      break;
    case 'mymodule_custom_date':
          .....
          break;
  }

  return $element;
}


You can use it anywhere and It uses the least resources.