2

Possible Duplicate:
Date popup form field doesn’t show the default value correctly

I'm trying to set a default value for my custom date_popup field. To be precise I'm trying tho set the field to the first day of the current month. But if I can populate the field without errors I can figure out the rest.

At first I was trying to input a string value:

#default_value = '1/1/2012'

This results in no errors but also the field will remain empty.

I've looked trough some issues including this one and noticed that I had to use a date() as value. But this also results in an empty field using the the following code:

'#default_value' => date('d/m/Y'),

Which I think should give todays date?

Finally I used the following code:

'#default_value' => array('day' => 1, 'month' => date('m'), 'year' => date('Y')),

which populates the field but gives me:

Warning: strlen() expects parameter 1 to be string, array given in date_default_date() (line 114 of /home/quickstart/websites/nieuwesite/sites/all/modules/date/date_api/date_api_elements.inc).

Of course I'm trying to build this field in hook_form(). for reference here is my whole field:

$form['click_stats_date_start'] = array(
'#type' => 'date_popup',
'#title' => t('Start date'),
'#label_display' => '',
'#date_format' => 'd/m/Y',
'#date_year_range' => '-1:+0',
'#date_label_position' => 'hidden',
'#default_value' => array('day' => 1, 'month' => date('m'), 'year' => date('Y')),
'#size' => 20,
'#required' => TRUE,
);

So my question is how do I populate the date_popup field on the first load without errors?

0
2

You are getting the "expects parameter 1 to be string" error because the only value the date popup form element accepts as default value is a string with the format described in date_popup_element_info().

Set the #type to date_popup and fill the element #default_value with a date adjusted to the proper local timezone in datetime format (YYYY-MM-DD HH:MM:SS).

The code you should use is the following one.

$form['click_stats_date_start'] = array(
  '#type' => 'date_popup',
  '#title' => t('Start date'),
  '#label_display' => '',
  '#date_format' => 'd/m/Y',
  '#date_year_range' => '-1:+0',
  '#date_label_position' => 'hidden',
  '#default_value' => date('Y-m-d'),
  '#size' => 20,
  '#required' => TRUE,
);

If you need to visualize a specific value as default value, then the code should be the following one. (Replace $saved_timestamp with the value you need to show.)

$form['click_stats_date_start'] = array(
  '#type' => 'date_popup',
  '#title' => t('Start date'),
  '#label_display' => '',
  '#date_format' => 'd/m/Y',
  '#date_year_range' => '-1:+0',
  '#date_label_position' => 'hidden',
  '#default_value' => date('Y-m-d', $saved_timestamp),
  '#size' => 20,
  '#required' => TRUE,
);
4
  • indeed date('Y-m-d') is working as well it was the date format that was causing my error not the added mktime() I will accept this answer
    – FLY
    Nov 28 '12 at 9:58
  • The date format I used is for the default value is Y-m-d because you are just showing those values; if you would show other values, the format to use should be Y-m-d G:i:s.
    – apaderno
    Nov 28 '12 at 10:19
  • now that you've edited drupal.stackexchange.com/questions/41012/… I think this is a duplicate should I close this one?
    – FLY
    Nov 28 '12 at 10:20
  • 1
    I think there is a difference between this question, and the other question. The other one asks for the string format to use as default value, while this question is implicitly asking if the form element accepts other values (e.g. an array). Keep in mind that closing a question is not a punishment: It serves just to redirect the users to the first question asked.
    – apaderno
    Nov 28 '12 at 10:34
1

you need to use date() with mktime(). The thread you referenced has the clue for you.

Here is the working example,

  $form['click_stats_date_start'] = array(
    '#type' => 'date_popup',
    '#title' => t('Start date'),
    '#label_display' => '',
    '#date_format' => 'd/m/Y',
    '#date_year_range' => '-1:+0',
    '#date_label_position' => 'hidden',
    '#default_value' => date("Y-m-d", mktime(0, 0, 0, date('m'), 1, date('Y'))),
    '#size' => 20,
    '#required' => TRUE,
  );
6
  • I see so it is required to input the 'optional' timestamp in the php date function in order to get it to work. Thats why I didn't see it many thanks!
    – FLY
    Nov 28 '12 at 9:29
  • 1
    There is no need to use mktime(): Just use date('Y-m-d').
    – apaderno
    Nov 28 '12 at 9:48
  • FLY wanted first day of the current month as default. Nov 28 '12 at 10:01
  • So it was not the mktime() that did the trick but the date format. Still +1 for the suggestion how to create the day of the first month. I would have figured this out myself the problem I was having is not able to fill in a default value without an error. Thats what @kiamlaluno answered, I think that's a better suited answer.
    – FLY
    Nov 28 '12 at 10:04
  • Then this should be a duplicate of drupal.stackexchange.com/questions/41012/… :( Nov 28 '12 at 10:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.