2

I need a "date of birth" field. Out of the box, the date picker only allows moving left/right by month, which doesn't work for selecting years.

Is there a way to configure the date picker that Craft uses? Or is there a better way to make a date of birth chooser?

Thanks!

2

I've done this by populating a dropdown field using the Dynamic Fields plugin. Here's the code I used to generate the dropdown options:

{% set maxYear = now|date_modify('+3 years')|date('Y') %}
{% for year in maxYear..1945 %}
    { "value": "{{ year }}", "label": "{{ year }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

Update:
Re-reading your question I just noticed that you might not only need a year picker. Well you could add two more dropdown fields for months and days (1-31) or you just teach your client to input the date into the normal Date/Time field with the keyboard.

| improve this answer | |
  • I was considering Dynamic Fields already (and putting 3 of them in a SuperTable to avoid using too much space). It would be nice to store the date in a single field though (and in proper date format). Thanks for the code! – Jure Srpcic Oct 30 '15 at 12:57
  • 1
    You could use the Preparse plugin to combine it to a single field. That's actually a good idea, especially if you're using the dropdowns in a Super Table / Matrix. Makes it easy to output in the template or even use the field as a field parameter in a criteria model. – carlcs Oct 30 '15 at 13:01
  • Good idea - being able to store age directly as a field. I'll add the final code as an answer for anyone dealing with a similar situation. – Jure Srpcic Oct 30 '15 at 13:28
  • 1
    Don't store the age. Store the day in 2015-10-30 format and you can use Twig's date filter in your templates to output in any format you want, even the weekday. – carlcs Oct 30 '15 at 13:31
  • Added as an option in comment. – Jure Srpcic Oct 30 '15 at 13:33
3

Using carlcs's idea, I put three LJ Dynamic Dropdowns in a SuperTable, here's the code for each dropdown (day, month, year):

{% for day in 1..31 %}
    { "value": "{{ day }}", "label": "{{ day }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

{% for month in 1..12 %}
    { "value": "{{ month }}", "label": "{{ month }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

{% set maxYear = now|date_modify('-15 years')|date('Y') %}
{% for year in maxYear..1925 %}
    { "value": "{{ year }}", "label": "{{ year }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

Then I added a Preparse field that joins these three dropdowns into a date string on each save (I have this under a User, so adapt to Entry/Asset/etc. if you need to).

{% spaceless %}
{% if user.userDateOfBirth | length %}

    {% set dateString = user.userDateOfBirth[0].year ~ '-' ~ user.userDateOfBirth[0].month ~ '-' ~ user.userDateOfBirth[0].day %}
    {{ dateString }}

{% endif  %}
{% endspaceless %}

To calculate age in my template, I use this little snippet (date("2015-07-06") will return a DateTime object so we can use the diff function on it).

{% set ageString = date(dateString).diff(now).format('%y') %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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