Drupal core offers a standard date field type, but it has no knowledge of a field type to represent (a certain moment in) time. The following snippet of code generates a Drupal 6 time element for you which you can use in all your forms. It consists of 3 select fields representing hour, minute and meridiem (like 12:01 PM).
An example of usage is the following:
$form['test_reset']['test_reset_time'] = array(
'#type' => 'test_time',
'#title' => t('Reset time'),
'#default_value' => variable_get('test_reset_time', array('hour' => 12, 'minute' => 0, 'meridiem' => 'pm')),
);
The code for the element is:
<?php
function time_field_elements() {
$type['time_field_time'] = array(
'#input' => TRUE,
'#process' => array('time_field_expand_time'),
);
return $type;
}
function time_field_expand_time($element) {
if (empty($element['#value'])) {
$element['#value'] = array(
'hour' => intval(format_date(time(), 'custom', 'h')),
'minute' => intval(format_date(time(), 'custom', 'i')),
'meridiem' => format_date(time(), 'custom', 'a'),
);
}
$element['#tree'] = TRUE;
foreach ($element['#value'] as $type => $value) {
switch ($type) {
case 'hour':
$options = drupal_map_assoc(range(1, 12));
break;
case 'minute':
$options = drupal_map_assoc(range(0, 59));
break;
case 'meridiem':
$options = drupal_map_assoc(array('am', 'pm'));
break;
}
if ($type == 'hour' || $type == 'minute') {
foreach ($options as $option) {
$options[$option] = str_pad($options[$option], 2, '0', STR_PAD_LEFT);
}
}
$parents = $element['#parents'];
$parents[] = $type;
$element[$type] = array(
'#type' => 'select',
'#default_value' => $element['#value'][$type],
'#attributes' => $element['#attributes'],
'#options' => $options,
);
}
return $element;
}
/**
* Implementation of hook_theme().
*/
function time_field_theme($existing, $type, $theme, $path) {
return array(
'time_field_time' => array(
'arguments' => array('element' => NULL),
),
);
}
function theme_time_field_time($element) {
return theme('form_element', $element, '<div class="container-inline">'. $element['#children'] .'</div>');
}
Comments
I wanted to implement this, but the code has been messed up, and it seems that #type = test_time has not been specified anywhere, so how can one use that directly in the form?
We have classes which meet at a certain time every day. I need a CCK field for "Time" which can do sorts, but the CCK Date module wants to have "Year, month, and day" in the granularity, which do not apply. You say that the Date module can do Time, but I have not found out how.
Hi Russ,
Just put this in your module. Starting from then you can start using the following in your FAPI forms:
$form['test_reset']['test_reset_time'] = array(
'#type' => 'test_time',
'#title' => t('Reset time'),
'#default_value' => variable_get('test_reset_time', array('hour' => 12, 'minute' => 0, 'meridiem' => 'pm')),
);
Regarding CCK, I can advise you the Date module which can do time too.
How can I implement this? Could this be made into a CCK field without too much trouble?
Post new comment