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

<ol>
<li><code>startDateTime</code>, <code>endDateTime</code> and php’s <code>date()</code> function were not returning the same timezone date &amp; time.</li>
<li>The correct formatting for matching multiple values needs to be in an array.</li>
</ol>

<hr>

<p><strong>The fix:</strong></p>

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

<p><code>date_default_timezone_set(craft()-&gt;timezone);</code></p>

<p>Then I reformatted my <code>criteria</code> settings as such:</p>

<pre><code>'criteria' =&gt; [
    'section' =&gt; 'mySection', 
    'type' =&gt; 'mySectionType',
    'startDateTime' =&gt; ['&lt;=' . date("Y-m-d H:i:s.u"), ':empty:'],
    'endDateTime' =&gt; ['&gt;=' . date("Y-m-d H:i:s.u"), ':empty:']
],
</code></pre>

CC BY-SA 3.0
2016-12-08T19:52:41.910Z
2016-12-08T19:52:41.910Z
Taylor
<p>What is the proper syntax in the <strong>ElementAPI 'criteria'</strong> 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.</p>

<p>I have the proper syntax working in my twig template:</p>

<p><code>.startDateTime('&lt;=' ~ now.date,':empty:').endDateTime('&gt;=' ~ now.date,':empty:')</code></p>

<p>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:</p>

<pre><code>'criteria' =&gt; [
    'section' =&gt; 'mySection', 
    'type' =&gt; 'mySectionType',
    'startDateTime' =&gt; '&lt;=' . date("Y-m-d H:i:s") . ', :empty:',
    'endDateTime' =&gt; '&gt;=' . date("Y-m-d H:i:s") . ', :empty: '
],
</code></pre>

<elementcriteriamodel><plugin-elementapi>
Limit ElementAPI results with custom date/time field
# Limit ElementAPI results with custom date/time field

(Asked by: username_0 on 2016-12-07)

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: '
],

**Tags:** elementcriteriamodel, plugin-elementapi

---

## 1 answers

### Answer by username_0 on 2016-12-08. Score: 2

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

startDateTime, endDateTime and php’s date() function were not returning the same timezone date & time.
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.
date_default_timezone_set(craft()->timezone);
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:']
],


---

