Hi! I think this should be relatively easy to do but I can't figure it out. I have a time field with hours and minutes. I need the minute field to only have options for 00, 15, 30, 45. Showing all 60 minutes is overwhelming. Can someone point me in the right direction?

I know that I could change my time field into two text select boxes, but I like how it says "hour" and "minute" within the select box and this isn't possible to do with just a plain text select box.

Comments

Title:Time - Minutes to 00, 15, 30, 45Time Increments: Minutes to 00, 15, 30, 45
Version:5.x-2.6»

New features are being added to the 3.x version only. I've closed the following issues as duplicates:
#586342: Time increment
#338449: Is it possible to customise the time field?

Version:» 6.x-3.x-dev

Was this feature added to the 6.x-3.x-dev version? That's the one I have but I don't see any options for increments. Instead I changed the time.inc file to this:

<?php
 
// Generate the choices for drop-down selects. Minutes are 15-minute increments
 
$hours[''] = t('hour'); //default option
 
for ($i = $first_hour; $i <= $last_hour; $i++) $hours[$i] = $i;
 
$minutes = array(
   
''   => t('minute'), //default option
   
'0'  => '00',
   
'15' => '15',
   
'30' => '30',
   
'45' => '45',
  );
?>

It works, but when a user selects '00' minutes the value is blank in the e-mail. Any idea why?

I don't know about writing patches, but I'm loving the fix suggested in #2. I didn't check the email output, but viewing the webform results shows the double zero in my D6.20 install with Webform 6.x-3.6.

Any chance this can get rolled into dev if it's not already there?

Any chance this can get rolled into dev if it's not already there?

We need a more extensive fix than just hard-coding the minutes in 15 minute increments. We'd need an option when configuring time components to set the increment. I'd be happy to look at patches, but this issue is not a high priority for me.

+1 for this feature via the user interface!

Also +1 for this feature via the user interface!

+1 subscribing...

Version:6.x-3.x-dev» 7.x-3.x-dev
Assigned:Unassigned» drclaw
Status:Active» Needs review
StatusFileSize
new2.36 KB

Thought I would take a stab at this one. Seemed pretty straight forward. Overview of the patch:

- Adds a setting on the time field page to select the minute increments (1, 5, 10, 15,30)
- Validates that the default time's minutes is a multiple of the increment setting
- Uses the minute increment settings to populate the time component options properly on the webform itself

Patch is for 7.x-3.x-dev but should be easily back ported to d6.

Hope it's helpful!

StatusFileSize
new2.35 KB

Oops... Sorry, just realized that the patch was for version 3.x-3.9-hotfix. Here's one that will apply to the 7.x-3.x-dev branch. Apologies!

Nice one @drclaw! I'll review when I get a chance.

Looking forward to it. =)

I definitely need the patch but I'm running D6. Can I apply this same patch?

I'm not sure if this will apply to the d6 version... Maybe give it a try and see what happens? Otherwise, it's a small patch and should be fairly easy to backport...

I'm taking a look at this code now. Overall it looks and works great! It'll need a little bit of tweaking to help make it easily compatible with Form Builder (we'll change the validation function to be on the minuteincrements form element, rather than on the entire form).

StatusFileSize
new4.63 KB

Here's a patch which takes a slightly different approach:

- Instead of validating the default value, it will simply round the default value to the next valid minute. This bothers the user less and solves problems with using dynamic times, such as "now" as the default value.
- Removed some legacy and cruft code.
- Removed redundant description/help texts.
- Switched to using a FormAPI attribute for #minuteincrements, to make it easily compatible with Form Builder.

Patch seems to apply to D6 and D7 just fine, so no porting necessary. How does this approach sound to you @drclaw?

Status:Needs review» Reviewed & tested by the community

Nice! I just gave it a whirl and it works perfectly! I like what you did with the time rounding. It works well. =)

Thanks!

I do not have time to test (meeting in 10 minutes...) but I looked at the patch.

<?php
for ($i = 0; $i <= 59; $i++) {
   if (
$i % $element['#minuteincrements'] == 0) {
    
$minutes[$i] = $i < 10 ? "0$i" : $i;
   }
}
?>

I suggest replacing this with
<?php
for ($i = 0; $i <= 59; $i += $element['#minuteincrements']) {
 
$minutes[$i] = $i < 10 ? "0$i" : $i;
}
?>

A few lines later, I see

<?php
  
foreach ($minutes as $minute => $padded_minute) {
     if (
$minute > $default_values['minute']) {
      
$default_values['minute'] = $minute;
       break;
     }
   }
?>

Again, I do not have time to test, and I do not have the context, but it looks as though the test should be
<?php
    
if ($minute >= $default_values['minute']) {
?>

Status:Reviewed & tested by the community» Needs work

Thanks @benjifisher, both excellent suggestions. Let's refactor and test again.

Status:Needs work» Reviewed & tested by the community
StatusFileSize
new4.6 KB

My second suggestion was half-baked. The code that I quoted is wrapped in

<?php
     
if (!isset($minutes[$default_values['minute']])) {
?>

so it should not matter whether you test for strict inequality or not (> or >=).

I was not sure how to test the code, so I just set the increment to 5 minutes and added

<?php
      $default_values
['minute'] = 3;
?>

to the code. It works. My minute field was set to '05'. I did some further testing, and 23:58 gets rounded up to 12:00 pm as expected.

I have attached a patch. The only difference from the one in #15 is my first suggestion from #17.

Status:Reviewed & tested by the community» Needs review

Sorry, I did not mean to mark my own patch RTBC.

Status:Needs review» Reviewed & tested by the community

Cool. Looks good. Just tested it out with no issues. Great suggestion btw. ;-)

Status:Reviewed & tested by the community» Fixed

Thanks guys! Tested again and works great. Committed to both 3.x branches of the project. Teamwork ftw!

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Issue tags:+hourly

We would like an option to increment hourly -- in this case, we would probably not need a minutes dropdown at all, just add ":00" to all of the hour options, or as plain text after.