Jump to content

How would you opt to do a date field with optional DD/MM?


cst989
 Share

Recommended Posts

I have a catalogue of DVDs in ProcessWire. Sometimes I know the exact date, sometimes I only know the month and year, or just the year. 

I'm migrating from a system that allowed me to put in the date like 00-00-1995, but PW's date fields will always default to 01-01-1995. Even if you use the date option that says "separate inputs for year, month and day" and only save year, it will still set the month and date to Jan 1st.

Ideally I want it to still behave like a date field, maybe with a calendar, but at least limiting the format to correct dates, and being able to sort by it on the front end.

I can think of workarounds, like having 3 separate fields and perhaps joining them together in a hidden field or in my template. But I wondered if anyone had a go-to approach that they've used for this in the past.

Thanks in advance! 🙂

Link to comment
Share on other sites

I think the simplest solution would be to use a regular text field and additionally create a hidden datetime field. Then you add a hook on saveReady that populates that datetime field via something like strtotime(...). But it depends who inputs dates... only you? Or also others? Then you need to be more careful with input and maybe provide 3 inputfields, one for day, month, year. That could also be done with a hook.

  • Like 2
Link to comment
Share on other sites

The question prompted me to put FieldtypeSketchyDate on GitHub. It's a small module I started writing for a solution that never went anywhere, so its pretty basic. It may be a starting point though.

In short, it lets you enter 2024 or 2024/10 or 2024/10/11 in the date value, and it also lets you search for 2024/10 and returns all pages with a date between 2024/10/01 and 2024/10/31 as well as those where 2024/10 was entered. So far, it just validates the formatting through the HTML pattern attribute. Proper date validation and calendar UI are still on the todo list.

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...