Drupal6 - Programatically create new date type and date format

Submitted by Janak on Tue, 11/22/2011 - 11:46

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;
}