I would like to create a CCK Time field (HH:MM), can it be done? If so, can you show me how to go about it?

Thanks

Comments

Rowanw’s picture

trantt’s picture

MM DD YYYY are required fields on the date module. In short, I was already tried out this module prior to this post.

vm’s picture

if its for something like an event time, it may be that you can use a few drop downs, one for hours, one for mins (or increments in 15 min blocks of time) and one for am pm.

it would help provoke answers if you explained what your intentions were with such a field?

samreenkhadim’s picture

i want few drop downs for time one for hours, one for mins (or increments or decrement time) and one for am pm on my form. Please help me out to solve this issue. Please someone help me.

vm’s picture

marcvangend’s picture

I'm also in need of a cck time field without a date. Trantt's statement is not completely true: the date module does allow a YYYY HH MM field, without month and day. However this is not good enough for me.

I want to display a list of gigs for a band and created a 'gig' content type. One of the fields I'd like to add is a 'doors open' time because this is usually different from the time the gig actually starts.

I also tried the method of creating two select lists (one for the hours 00-23 and one for the minutes 00/15/30/45) but I dont like the way they are placed below eachother in the create content form, it's not user friendly.

Hopefully the date module will make this possible in the future.

nishitdas’s picture

Dont have any answer but this is exactly the same thing I am looking for. To figiure out exactly how much time is a user on a page and then come up with a message after a particular time

For example - An user is giving a test/quiz which is to meant to be taken in 15 mins. I would love to have a CCK field which shows how much time the user is on the page and after 15 mins it shows a message (pop up) your time is over.

trantt’s picture

"An user is giving a test/quiz which is to meant to be taken in 15 mins" I have been looking for this modules for several months now but had no luck so far. Please let's me know if you came up with a solution for this.

davidburns’s picture

I am also looking for a module to handle this.

I need an opening time and closing time for each day of the week for all the gaming centers that sign up for a site I am working on.

alvarezjt’s picture

This is the same thing with me. I'm creating a movie site, and would like to have a time field to enter the Running Time of each movie, and to help the editors to maintain a standard for entering the time, which is one of my reasons for needing a time field. I thought the date module could do this, but I'm so lost right now.

Can someone help please?

Sig:
Every day begins a new journey...

dnguyen’s picture

I see this issue is mostly resolved with custom php-like time formats...however, I have a somewhat related question. I want businesses to be able to have opening and closing times...by day. Any suggestions on how to use CCK fields to do this? Other than the most obvious and clunky way, which is to have 7 groups, each consisting of two time fields?

Wyze1’s picture

I would suggest this:

Have a normal field text field eg.

$form['whatever']['field'] = array(
'#title' => t('Time'),
'#type' => 'value',
'#type' => 'textfield',
'#default_value' => '00:00am',
'#required' => true
);

then validate the field like this:

function formname_validate($form_id, $form_values) {
if (!ereg('(^([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])(\s{0,1})([AM|PM|am|pm]{2,2})$)|(^([0-9]|[1][0-9]|[2][0-3])(\s{0,1})([AM|PM|am|pm]{2,2})$)', $form_values['field'])) {
form_set_error('field',
t('Please Input Your Time In The Correct Format 00:00am.'));
}

itahead’s picture

When you add date field to your content type. There is heading Granularity: just select Hour and Minute. This will display hour and min drop down when you create your content.

There are other options how to display this. Configure those and you are done.

marcvangend’s picture

itahead, the issue is not that a granularity of HH:MM is not available. The point is, that you have to include at least a YYYY field. Sometimes, you just want hours and minutes, without years and months. That is not possible with the date module right now.

itahead’s picture

I added field in my content type as date (Text using strtotime), and later I selected input format / output format as H:i and selected Hour and Min from granularity.

And its working for me.

marcvangend’s picture

I agree, that is a possible workaround. I can see myself using it for sites I am maintaining myself. However I would never deliver this to a paying customer, it would be below my quality standards. (No offense intended, itahead, I don't know in which situation you used it and I'm not judging the fact that you did.)

maozet’s picture

Add a field as date (Select List, ie: the first option usually) and later change the custom output format to H:i.
This will create two select list, one for HOURS and one for MINUTES

regards,

remtheory’s picture

I'm surprised we're forced to add in a date and can't just do time. Seems like a weird limitation and that it could be a simple fix (or is that just wishful thinking)?

dagomar’s picture

Dagomar Paulides
B.A. Digital Media Design
Partner @ Online Agency

remtheory’s picture

Thank you!!

greg.harvey’s picture

Just what I was after. Shame the Date module doesn't handle this easily. Sounds like the strtotime option will, but it's not ideal. This module is perfect. =)

blowski’s picture

Unfortunately, CCK Time doesn't work with 6.x.

There is also the Office Hours module, which may solve one of the problems here. However, it forces day, opening and closing time so will not be a universal solution.

Dan Blows

chriscalip’s picture

Folks, Feel free to use: http://drupal.org/project/duration for your time range needs. :)

greg.harvey’s picture

Will take a look when I get a chance. Thanks for contributing a well needed module! =)

capellic’s picture

It's a nice looking module for what it does, but we're looking for a way to have start and end times.

no2e’s picture

.

problue solutions’s picture

Is there a module or solution for 6.x?

Duration module is not what i'm looking for.

chappesepp’s picture

Hi folks,
I solved it with the Date-Modul. Just remove or uncomment in the date_admin.inc the validation code for the year granularity

if (!in_array('year', $field['granularity'])) {
    form_set_error('granularity', t('Granularity must include a year.'));
}

In the Date_6.x-2.3 Version you will find the code in line 238. Works perfectly fine to show just hours and minutes.

aubjr_drupal’s picture

This is my 1st drupal.org post... This is assuming that you have the same modules installed as me (Date, etc.), so I make no guarantees, but after you've set up your Date field in your content type (I was making a custom content type with Date, CCK, etc.), set the granularity, etc., create your own custon format and apply it. To do this, do the following:

1) Go to Site Configuration -> Date & Time -> Formats -> Add format
2) In "Format String", enter your custom PHP date code (taken from http://php.net/date - I used g:i A for "9:30 AM", etc.)
3) Go to Configure under the same level of Drupal navigation (under Formats).
4) Either create a new custom format (under Add Format Type at the bottom) and then select your new format from the drop down list, or change one of the existing formats to your new format.
5) Go back to your content type's DateTime field (whatever it is named) and change the Default Display to the format you chose in #4.

This worked like a charm for me, without any module hacks to remove the YYYY, etc.

marcvangend’s picture

That's a perfectly good solution on the output side (and similar to http://drupal.org/node/134144#comment-718549, yet better explained) but if I understand correctly, it will not change anything on the node form (input side). This means that the person who enters the node form to create a new node, will still have to select a year when he just wants to enter the opening time. So to me it's only one half of the solution.

ssg13565’s picture

Changing the input side is easy. I fixed that before I came looking for a solution on the output side.

When you configure your date field just create a custom input format.

I used

h i a

for my purposes so that I no longer had to specify a year. However, the year seems to have defaulted to the current year.

That did not fix the display, which still showed the year. That is why I am looking at this suggestion for fixing the output format.

marcvangend’s picture

Like you say, a year value is still stored in the database. That means you still can't perform certain actions with that value, such as sorting values chronologically.

Don't get me wrong, I'm not criticizing the solution that you chose in your specific situation. All methods described in this thread are usable workarounds for certain use cases. My personal itch is that there isn't a generic solution that would always work - but having said that, I must admit that I didn't scratch it myself either.

Anonymous’s picture

subscribing

gavranha’s picture

@aubjr_drupal : that was a good workaround. Works for me. Thanks for the tip!

ssg13565’s picture

Thanks for the great tip. This is just what I needed. I now have a separate event date field and a separate event time field. I fixed all my events and all my views.

capellic’s picture

THIS IS GREAT!!!

One thing to add here is that you need to set the default value on the field in question.

Default value: Blank
Default value for To date: Blank

Customize Default Value fieldset

Custom value for From date: +0 days

This will result in you getting an empty select list and take care of filling the YYYY field with the current year.

yesct’s picture

how to default to a particular year? so that next year, if someone enters in a time, it does not use 2012 for the year, but keeps the same year for ever? Like I would just specify 2010 and hours and minutes and then ignore the year for display?

pwaterz’s picture

If you set the input format to just be time and then set it to be required. The date module looks for the year field and if it can't find it, it throws a validation error. Also even if it's not set to required. It does not save the times I enter.

PJW

malikilam’s picture

At first the solution offered by aubjr_drupal didn't work for me either. Then I thought to try turning off the Clientside Validation module I was using and it worked like a charm. I hope that little nugget helps somebody. Cheers

problue solutions’s picture

pwaterz is correct, this does not work for the reasons he has stated above.

Anyone who thinks they have this working have somehow got what they needed without realising that what we really require is more complex.

cj-a-min’s picture

Accept for Wyze1s work around, none of these attempts listed here offer a solution. Your only masking the data, by not displaying it. The data is still being stored in DB with year-month-date hours:minutes.

2011-01-01 16:11:00

This data stored is completely useless, if you can't process it; create meaningful information!!!!

No matter how you save your data, 'Date' will always store year-month-date hours:minutes.

This module obviously needs to be patched in the next release, or unless this is by design. Which I don't think it's by design, as 'datetime' field type should be able to do solely, time.

CCK Time, is an alternative module and works, but is not supported by 'Node Imports'; you can't map the "cck time" fields with your imported CSV/XML. - If this is solved, than CCK Time would be the winner to store 'time'.

marcvangend’s picture

You sound very upset about all of this, which doesn't seem very respectful to the people that have tried to come up with solutions and workarounds. If the current code is not up to your standards yet, feel free to submit patches. If you contribute, everybody wins.

I don't agree that data stored with year and month is completely useless. Suppose you're storing opening hours of stores. If you make sure that all opening hours are stored on the same year and date (for instance the Unix epoch 01-01-1970), you can still process that data. For instance you could sort all stores by opening time, or select stores that close after 8PM.

operations’s picture

Thanks to dkinzer on the solution:

http://stackoverflow.com/questions/3753782/drupal-cck-date-field-with-ju...

You can define a custom format in the date settings and use it in the field settings. Just use the format h:i and keep the hour Granularity selected and don't worry it won't be displayed.