Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This post explains how to add a field representing the length of piece of music in minutes and seconds (eg. 35m 05s). Other may find it useful too.
- Ensure that CCK and the Date module are enabled.
- First, we define a new date format that displays as, for example, 05m 35s.
- Go to Admin, and in the Site configuration section, select Date and time.
- Click Formats, and then Add Format
- In the Format string field, enter: i\m s\s (where "i" represents minutes, "s" seconds, and the \m and \s are suffixes)
For a longer suffix, for example, enter: i\m\i\n s\s\e\c (\m\i\n = min and \s\e\c=sec)
And click Save Configuration - If you click Custom Formats, you can see your new format listed
- Click Configure
- In the Add format type section, enter in the name field "Duration" and in the Type field, enter "duration" and click Save Configuration
- Still on the Configuration tab, you'll see after the Long, Medium and Short date formats, your new format called "Duration". From the select box, you will be able to select your new date format, showing as, for example, 20m 35s (the actual number will depend on the current time)
- Click Save Configuration
- Go to Content Types, and for the type you want to add a "duration" field, click Manage Fields.
- In Add New field, enter a new field label (eg. duration), the field_ name, (eg. so it reads field_duration)
- Select the field type of datetime, and from the select list, Text Field with custom input format
- On the new duration field configuration screen
- In the Customize Default Value section, enter: 1 January 2000 0:0:0
This sets a default common starting date and time, most of which will be ignore, but will ensure that if we want we sort this field - In the Custom input format field, enter: i:s
Which requires the duration to be entered as: minutes : seconds
If you wish, you could add to this field's Help text: Minutes : Seconds - In granularity, ensure that all are selected, including "seconds"
- In Default Display, you should find your new date format available called "duration"
- In the Customize Default Value section, enter: 1 January 2000 0:0:0
- And that's it, and the field should be available in Views too.
Comments
Comment #1
amitsedaiz CreditAttribution: amitsedaiz commentedGreat.... Thanks. Had been looking for a solution like this. Just a query... if my format is H:i , is there a possibility to add am or pm in the 12 hour format?
Comment #2
iantresman CreditAttribution: iantresman commentedYes, the format is specified by the PHP date format, so to add am or pm, change the format string from "H:i" to "H:i a".