1

I have custom installation profile and I must set date formats programmatically.

I tried:

db_insert('date_formats')
    ->fields(array(
        'format' => $format_long,  // PHP Date String
        'type' => 'custom',          // 'custom'
        'locked' => 0,               // 0 = user-specified date format
    ))
    ->execute();
variable_set('date_format_long', $format_long);

and

$format_long = 'l, Y-m-d G:h';
system_date_format_save(array(
    'is_new' => 1,
    'type' => 'custom',
    'format' => $format_long,
    'locked' => 0,
));

and set this up:

   variable_set('date_format_long', $format_long);
   variable_set('date_format_medium', $format_medium);
   variable_set('date_format_short', $format_short);

At admin/config/regional/date-time all seems to be fine but:

time is e.g 16:04 (now I have 16:38). Time changing, but on settings page time is still the same (after refresh).

I tried to create a content and at admin/content listing I have content updated date as 16:04 too... :(

If I switch to default formats all works ok.

What is wrong?

EDIT

At admin/config/regional/settings time is 16:04 too....

EDIT

I created module based on suggestion from answer, still no luck :(

define('RPKE_DATE_FORMAT_LONG', 'l, Y-m-d G:h');
define('RPKE_DATE_FORMAT_MEDIUM', 'D., Y-m-d G:h');
define('RPKE_DATE_FORMAT_SHORT', 'Y-m-d G:h');
define('RPKE_DATE_DATE_TYPE_LONG', 'rpke_date_long');
define('RPKE_DATE_DATE_TYPE_MEDIUM', 'rpke_date_medium');
define('RPKE_DATE_DATE_TYPE_SHORT', 'rpke_date_short');



/**
 * Implements hook_date_format_types().
 */
function rpke_date_date_format_types() {
    $types = array();
    $types[RPKE_DATE_DATE_TYPE_LONG] = t('Long date format for RPKE');
    $types[RPKE_DATE_DATE_TYPE_MEDIUM] = t('Medium date format for RPKE');
    $types[RPKE_DATE_DATE_TYPE_SHORT] = t('Short date format for RPKE');
    return $types;
}

function rpke_date_date_formats() {
    return array(
        array(
            'type' => RPKE_DATE_DATE_TYPE_LONG,
            'format' => RPKE_DATE_FORMAT_LONG,
            'locales' => array(),
        ),
        array(
            'type' => RPKE_DATE_DATE_TYPE_MEDIUM,
            'format' => RPKE_DATE_FORMAT_MEDIUM,
            'locales' => array(),
        ),
        array(
            'type' => RPKE_DATE_DATE_TYPE_SHORT,
            'format' => RPKE_DATE_FORMAT_SHORT,
            'locales' => array(),
        ),
    );
}

function rpke_date_flush_caches() {
    _rpke_date_purge_date_formats();
}

function _rpke_date_purge_date_formats() {
    $format = system_get_date_formats(RPKE_DATE_DATE_TYPE_LONG);
    variable_set('date_format_' . RPKE_DATE_DATE_TYPE_LONG, RPKE_DATE_FORMAT_LONG);
    if (!empty($format['dfid'])) {
        system_date_format_delete($format['dfid']);
    }
    $format = system_get_date_formats(RPKE_DATE_DATE_TYPE_MEDIUM);
    variable_set('date_format_' . RPKE_DATE_DATE_TYPE_MEDIUM, RPKE_DATE_FORMAT_MEDIUM);
    if (!empty($format['dfid'])) {
        system_date_format_delete($format['dfid']);
    }
    $format = system_get_date_formats(RPKE_DATE_DATE_TYPE_SHORT);
    variable_set('date_format_' . RPKE_DATE_DATE_TYPE_SHORT, RPKE_DATE_FORMAT_SHORT);
    if (!empty($format['dfid'])) {
        system_date_format_delete($format['dfid']);
    }
}

EDIT

The worst nightmare is that creating custom date format from UI (e.g. custom date format in Views) result display of wrong date too....

1 Answer 1

3

Instead of manually inserting stuff in the database, make use of the Drupal API. The blog post Exporting and maintaining custom date formats in Drupal 7 is a great tutorial. It's geared towards modules but I'm pretty sure you can do this in an install profile too.

I think this will solve your problem, and even though I'm not 100% sure, I would always recommend to use the API.


Here's a quick copy-paste of the most important code from that blog post, just in case the link would break in the future:

/**
 * Define and export date formats.
 */

// Give the date format type a machine name.
define('MODULENAME_DATE_TYPE', 'MODULENAME_date_type');

// Define the string to be passed to date().
define('MODULENAME_DATE_FORMAT', 'l, jS M Y ');

/**
 * Implements hook_date_formats().
 */
function MODULENAME_date_formats() {
  $formats = array();
  $formats[] = array(
    'type' => MODULENAME_DATE_TYPE,
    'format' => MODULENAME_DATE_FORMAT,
    'locales' => array(),
  );
  return $formats;
}

/**
 * Implements hook_date_format_types().
 */
function MODULENAME_date_format_types() {
  $types = array();
  $types[MODULENAME_DATE_TYPE] = t('Default date for MODULENAME');
  return $types;
}

The blog post also provides a utility function to clean up old, unused date formats:

/**
 * Delete cruft date formats from the database and resync date formats/types.
 *
 * Call this as often as required to ensure date configuration stays in-sync.
 * Don't call this on every page load. hook_flush_caches() or hook_update_N
 * are good candidates.
 */
function _MODULENAME_purge_date_formats() {
  $formats = system_get_date_formats(MODULENAME_DATE_TYPE);
  variable_set('date_format_' . MODULENAME_DATE_TYPE, MODULENAME_DATE_FORMAT);

  // Date formats without a dfid are being read directly from code. We can use
  // the existence of this value to detect "ghost" formats that are not managed
  // in this file.
  if (!empty($format['dfid'])) {
    system_date_format_delete($format['dfid']);
  }
}
3
  • Thanks, I edited my question. However still no luck :(
    – Codium
    Commented Apr 29, 2013 at 17:17
  • 1
    I believe the link for the post has moved to godel.com.au/blog/…
    – Mark
    Commented Oct 15, 2014 at 3:51
  • You're right. I updated the answer. Please note that you can edit and improve answers yourself too! Commented Oct 16, 2014 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.