Drupal6 - Programatically create new date type and date format

If you are working with code based deployments instead of using CMS to push out changes, you might have already implemented something similar.

There are 2 approches to implementing this:

1) Define and save date formats

function mymodule_custom_date() {
  $date_format_monthday = 'm-d';
 
  $format = array();
  $format['format'] = $date_format_monthday;
  $format['type'] = 'custom';
  $format['locked'] = 0;
  $format['is_new'] = 1;
  date_format_save($format);  
 
  $format_type = array();
  $format_type['title'] = 'month_day';
  $format_type['type'] = 'month_day';
  $format_type['locked'] = 1;
  $format_type['is_new'] = 1;
  date_format_type_save($format_type);
 
  variable_set('date_format_'. $format_type['type'], $date_format_monthday);  
}

2) Implement date format and date type hooks

function example_date_formats() {
  $date_format_monthday = 'm-d';
 
  $format = array();
  $format['format'] = $date_format_monthday;
  $format['type'] = 'custom';
  $format['locked'] = 1;
  $format['is_new'] = 1;
 
  return $format;
}

function example_date_format_types() {
 
  $format_type = array();
  $format_type['title'] = 'month_day';
  $format_type['type'] = 'month_day';
  $format_type['locked'] = 1;
  $format_type['is_new'] = 1;
 
  return format_type;
}


Bookmark and Share

1 comment

Anonymous's picture

Thanks so much for this. I used the first method in my install profile and it worked flawlessly.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <apache>, <bash>, <c>, <cpp>, <css>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>, <vim>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options