0

I want to get only entries with a specific date. I created a date field customDateField. It has the following code:

{% set entries = craft.entries.section('orders').customDateField('2016-04-14') %}

but this not work. How it will look in php?

In element api plugin i was code:

'endpoints' => [
    'orders.json' => [
        'elementType' => 'Entry',
        'criteria' => [
            'section' => 'orders',
            'date' => '>='.craft()->request->getPost('date')
        ],
        'transformer' => function(EntryModel $entry) {
            return [
                'id' => $entry->id,
            ];
        },
    ],
]

but it only shows a larger date of entries, need to write 'date->format("Y-m-d")'

1

You don't need any extra php code to do that.

The search function probably does the job for you.

{% set entries = craft.entries.section('orders').search('2016-04-14') %}
{% for entry in entries %}
  <h1>{{entry.title}} - {{entry.customDateField}}</h1>
{% endfor %}
| improve this answer | |
  • php for the need to place the code in the element api plugin – Vitaliy Badalov Apr 7 '16 at 13:16
1

I wonder if you're getting caught out by querying a specific time of day. I think if you don't include the time it would presume '00:00'. What you want is to find any entries with the customDateField value between midnight and 23:59.

{% set myDateString = '2016-04-01' %}
{% set myDateStart = myDateString | date_modify('00:00') | date('Y-m-d H:i') %}
{% set myDateEnd = myDateString | date_modify('23:59')  | date('Y-m-d H:i') %}

{% set entries = craft.entries.section('orders').customDateField('and', '>= ' ~ myDateStart, '<= ' ~ myDateEnd) %}
| improve this answer | |
  • there between is not necessary – Vitaliy Badalov Apr 7 '16 at 13:13
  • Although this code is also useful, thank you! – Vitaliy Badalov Apr 8 '16 at 0:39

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.