Form API: fomatting the date select lists? I'd like to format my date select input, using the following time formats:


12-hour format of an hour without leading zeros
Minutes with leading zeros


Here's my form definition:

  $form['scheduled_for'] = array(
    '#type' =&gt; 'date_select',
    '#title' =&gt; t('Scheduled for'),
    '#date_label_position' =&gt; 'within',
    '#date_format' =&gt; 'd-F-Y g:i',
    '#date_increment' =&gt; 15,
    '#date_year_range' =&gt; '-0:+3',
    '#default_value' =&gt; time(),
  );


However, the hours format as 24-hour format of an hour without leading zeros (code G).

It's as if 'g' is being turned into 'G'. Any ideas? Thanks.
In date_parts_element() there's the line

$hours_format  = strpos(strtolower($element['#date_format']), 'a') ? 'g': 'G';


This is deciding on whether to be 24 hour on the presence of the AM/PM specifier. If you change the format to d-F-Y g:i a it should work I think.

If you don't want to display the AM/PM specifier, but fix it at a particular value, you can do something like this

  ...
  // Add a new process function to this date_select to hardcode am/pm.
  $info = element_info('date_select');
  $process = $info['#process'];
  $process[]= 'MYMODULE_date_select_process_am_only';

  $form['scheduled_for'] = array(
    '#type' =&gt; 'date_select',
    '#title' =&gt; t('Scheduled for'),
    '#date_label_position' =&gt; 'within',
    '#date_format' =&gt; 'd-F-Y g:i a',
    '#date_increment' =&gt; 15,
    '#date_year_range' =&gt; '-0:+3',
    '#default_value' =&gt; time(),
    '#process' =&gt; $process,
  );
  ...

/**
 * #process callback for a date_select element that removes the AM/PM specifier
 * and hardcodes its value to AM.
 */
function MYMODULE_date_select_process_am_only($element) {
  $element['ampm']['#type'] = 'value';
  $element['ampm']['#value'] = 'am';

  return $element;
}


It's untested but works in my head (: If you wanted to do the same thing on a number of different forms, there's a more elegant way (let me know if you're interested).