What is the proper syntax in the ElementAPI 'criteria' configuration settings for limiting entries based on custom start and end date/time fields. I can't use the default postDate and expiryDate due to localization requirements.

I have the proper syntax working in my twig template:

.startDateTime('<=' ~ now.date,':empty:').endDateTime('>=' ~ now.date,':empty:')

How can I mimic this same logic in the ElementAPI config settings? This is what I have so far. It doesn't throw any php errors but doesn't return the results I am expecting:

'criteria' => [
    'section' => 'mySection', 
    'type' => 'mySectionType',
    'startDateTime' => '<=' . date("Y-m-d H:i:s") . ', :empty:',
    'endDateTime' => '>=' . date("Y-m-d H:i:s") . ', :empty: '

After some exploration and trial and error, I was able to find a solution. Ultimately there are two problems:

  1. startDateTime, endDateTime and php’s date() function were not returning the same timezone date & time.
  2. The correct formatting for matching multiple values needs to be in an array.

The fix:

At the top of my elementapi.php file I added this line to make sure php’s date() function returns the same timezone craft has been set to in the admin config.


Then I reformatted my criteria settings as such:

'criteria' => [
    'section' => 'mySection', 
    'type' => 'mySectionType',
    'startDateTime' => ['<=' . date("Y-m-d H:i:s.u"), ':empty:'],
    'endDateTime' => ['>=' . date("Y-m-d H:i:s.u"), ':empty:']
| 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.