Creating a date format by program means in Drupal

Transferring configurations of one working site copy to another may often present difficulties. Particularly, date formats are not always correctly transferred with module features.

Below, we show how to create (transfer) a date format out of the existing code. The following two hooks should be used: hook_date_format_types() and hook_date_formats().

Let’s declare a new date format.

function mymodule_date_format_types() {
  return array(
    'my_format'  => t('My format'),

Next, we format our format:

function mymodule_date_formats() {
  $formats = array();
  $formats[] = array(
    'type'    => 'my_format',
    'format'  => 'M j',
    'locales' => array(),
  foreach ($formats as $format) {
    variable_set('date_format_' . $format['type'], $format['format']);
  return $formats;