Hi, I'm creating a new module, I have a field named "Due Date", which need three select drop down in mm/dd/yyyy format.
So at first, I use
$form[due_date] = array('#type' => 'date', ......);
And it gives me a nice date three dropdown selection. But the problem is, it default to today's date, but what I really need is default to the date two weeks from today. So I'm thinking of using "default_value" attribute to change the default date. But no matter how I tried, it just won't work. Can anyone give me some suggestion on how to properly use the default value? what's the format requirement? I searched a lot on site, but not much infomation. I'm really new to drupal, Thanks so much for any suggestion.
Also besides this problem. I also can't find a way to do error checking for the select dropdown. I can only get the error message on the top, but can't get the error red box surrounding the select drop downs like it does with textarea and textfield....
Comments
Can someone give me some feedback for this please?
what's the '#default_value' format for the date type in
$form[..] = array('#type' => 'date', '#default_value' => ...);
Default values for the new date type
Doing this:
'#default_value' => array('day'=> 1,'month'=>1,'year'=>2003)
gave me a default value of Jan 1, 2003 (for example).
Maybe there's a better approved way to set the default that doesn't rely on knowing the internal structure of the date variable, but I don't know what that would be.
try #value instead.
try #value instead.
from form.inc:
<?php $form['due_date'] =
Of course, I've hardcoded the values, so to actually figure out the date from 2 years ago you probably want to do this:
** I haven't tested the localtime() call but I recall having used it in the past.
-- Merlin
[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]
-- Merlin
[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]
My way of dealing with date element
2 function I'v added :
and now - to give the elemnt defulte value I use :
And for saving it in DB I use:
I think the expand_date($element) function sould suport data in timestamp format as well.
..:| Tomer Fish |:..
fatFish - Lean Mean Coding Machine
I suggest you put this code
I suggest you put this code snippet in the handbook. Very useful.
Suuch Solutions supports The GhanaThink Foundation
4.7.5 problems with this code?
I have been using this code for a little while on 4.7.4 with no problem. Now I think that at 4.75. my days are off by one when I go back to edit that change.
Anyone else see this?
Freekin' Timezone
This is a way more systemic problem at: http://drupal.org/node/65210 - But this did fix the problem for me by modifying your functions as so:
and
36000 being the specific timezone offset in seconds for my server.
Some More Helpful Functions For Dates
Hi, I installed Javascript Tools module and enabled jscalendar for some of my forms. Here some ideas for the conversion to timestamp when using jscalendar.
An important detail: #type must be textfield, I had troubles at first because foolishly declared #type as date.
These are just parts of code so you have to put them in context for your own code.
The first condition checks if this is a form for editing an existing record and retrieves the date set, if it's a new record then uses today's date.
I'm using a field called txnid (which is short for transaction id), what you may use depends on your table.
Then I have this function:
And use this for inserting:
and this for updating:
Of course this is SQL which will change for you particular table definition.
I hope it helps.
Cheers.
Alexis Bellido
Visit my new Drupal-based site on music, movies and books.
Thanks for pointing this out
Any idea why the database type a text field, why's it not converted into a date or at least an integer like "created" is? If I use #type => "date", what format does that correspond to in the database?
UPDATE: I'm using snippets as above, ta, surprised it's not built in though (at least as far as I've noticed)
Trying to code in the drupal way
I had a similar problem. I have a module which wants to track date/time information. Just like drupal, I made the date/time field in the database an integer with the intention of holding a UTC timestamp, and then using the Drupal functions to properly display it for the users.
I ended up with the following functons:
Then, in the form:
And in the submit function:
pls try this
when i need to add a datee filed in my module i used this code
// Provide a format using regular PHP format parts (see documentation on php.net).
// If you're using a date_select, the format will control the order of the date parts in the selector,
// rearrange them any way you like. Parts left out of the format will not be displayed to the user.
$format = 'Y-m-d';
$date = date($format);
$form['date2'] = array(
'#type' => 'date_popup', // types 'date_text' and 'date_timezone' are also supported. See .inc file.
'#title' => 'select a date',
'#default_value' => $date,
'#date_format' => $format,
'#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
'#date_increment' => 15, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
'#date_year_range' => '-3:+3', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
);
i got this from
http://drupal.org/node/292667
only diffrence i made is to generate a popup calender by changing '#type' =>'date_select' to '#type' => 'date_popup'
depened on date, date popup modules.