4
votes

In creating a drupal form, one can easily create a date field.

'#type' => 'date',

But what about a time field? Is there an easy way to create and use a drupal form field type that describes the time of day? (Something more elegant than just a textfield with the label "time"?)

3

3 Answers

7
votes

Drupal 7:

date_popup in the date module makes really nice time fields You can use a form element like:

'#type' => 'date_popup',

'#date_format' => 'g:i a',

and the field will match the format you use. If you don't include date elements, it will just show time.

4
votes

Actually, the date field is nothing more than a textfield with a label. That is on the outside. What you do get when using this field from the date module, is a lot of date validation, to make sure that what the user has typed/selected makes sense. The other thing you get is some different javascript tools to aid the typing of the actual date. But to do this, you need the data module.

Since time is a lot more simple (universal) than a date, you wont need much validation. So all you need, to make it into a fancy time field, is to add some javascript to help out, but this is not really needed.

So the short answer is no, you can't do something like that, but you don't really need to either. You could create your own time field with hook_elements() though if you really wanted to.

3
votes

There does not seem to be a 'standard' solution for this at the moment, but you might find something useful in the following:

And while not exactly providing a time field, you might also be interested in the duration modules, depending on what you want to achieve.