Creating date field with only month and year - Drupal
Asked Answered
E

4

6

I am creating a credit card form in Drupal. I need a date select field where users can select their credit card expiry date, which consists of only Month and Year -No Day.

The Drupal form #type 'date' produces a date chooser, which has day-month-year option. I just need month-year. Any help?

Endsley answered 8/3, 2011 at 7:36 Comment(0)
E
1

Hey, I found the correct way of doing this. The solution creates a custom form #type element. See http://www.akchauhan.com/create-drupal-form-elements-like-date-element/

Endsley answered 8/3, 2011 at 8:55 Comment(2)
By the way did you consider using the actual Date module with granularity set to only month and year?Sukkah
I didn't know about it. I'll try that. Thanks.Endsley
D
9

Using the Date API module (part of the Date module), you can do something like this....

<?php
$form['payment_expirationDate'] = array(
    '#type' => 'date_select',
    '#title' => t('Expiration Date:'),
    '#date_format' => 'm-Y',
    '#default_value' => $expirationDate,
    '#date_year_range' => '-1:+10',
    '#required' => TRUE,
    '#date_label_position' => 'within'
 ); 
?>

The date_select type will give you select box form elements for your field. And the #date_format array key allows you to use a PHP date format string to control what select boxes appear and what goes inside them.

Dumdum answered 2/3, 2012 at 23:1 Comment(0)
C
3

Go for two select boxes and pre populate the fields with month and years!

'#type' => 'select', '#options' => array( '1' => 'January', '2' => 'February', . . . '12' => 'December', ),

Same way for the years. You can generate the year array using simple php code and then use it in the option instead of giving all the years manually!

Charitycharivari answered 8/3, 2011 at 8:2 Comment(1)
Thanks for the suggestion. It is definitely a usable solution.Endsley
T
2

I've just done this in Drupal 7. Just install and enable the Date module, add a Date field to your Content Type, and then when editing it's settings, under "Date attributes to collect", just tick Year and Month. Then when a user is creating a new instance of that type, they will just get 2 dropdown boxes; one for Month and one for Year.

As for displaying them nicely, first go into Configutation > Date and Time settings, then create a new Format using PHP Date formatting e.g. "F Y" will give you something like "April 2012". Create a new Date Type which uses that format, and then go back to your Content Type display settings, and edit your new Date field to use the new Date Type.

Tripitaka answered 18/4, 2012 at 15:47 Comment(0)
E
1

Hey, I found the correct way of doing this. The solution creates a custom form #type element. See http://www.akchauhan.com/create-drupal-form-elements-like-date-element/

Endsley answered 8/3, 2011 at 8:55 Comment(2)
By the way did you consider using the actual Date module with granularity set to only month and year?Sukkah
I didn't know about it. I'll try that. Thanks.Endsley

© 2022 - 2024 — McMap. All rights reserved.