<p>You call it a bit of a hack, but I think the way you're doing it is the proper way:</p>

<pre><code>$criteria-&gt;dateCreated = '&gt;= ' . $fromDate-&gt;format(DateTime::MYSQL_DATETIME);
$criteria-&gt;dateUpdated = '&lt;= ' . $toDate-&gt;format(DateTime::MYSQL_DATETIME);
</code></pre>

<p>Your last example would work (with a syntax change) if you were you looking for a date range within a single field (either dateCreated or dateUpdated) like so, but not across two fields:</p>

<pre><code>$criteria-&gt;dateUpdated = array('and', '&gt;='.$fromDate-&gt;format(DateTime::MYSQL_DATETIME), '&lt;='.$toDate-&gt;format(DateTime::MYSQL_DATETIME));
</code></pre>

CC BY-SA 3.0
2015-09-20T09:18:23.567Z
thanks brad, I knew I was close, thinking back I was probably doing something silly like having a < wrong way round
andrew wisener
CC BY-SA 3.0
2015-09-16T20:54:15.190Z
2017-05-11T06:11:44.787Z
2017-05-11T06:11:44.787Z
Brad Bell
<p>I'm sure there must be an easy way to do this, but I can't work it out...</p>

<p>I'm trying to add date filters to the formerly submission export, but I can't work out how to filter out the submissions between 2 dates. I got it working with a bit of a hack:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria('Formerly_Submission');
$fromDate   = craft()-&gt;request-&gt;getPost('fromDate');
$fromDate = DateTime::createFromString($fromDate,   craft()-&gt;timezone) ;
$toDate   = craft()-&gt;request-&gt;getPost('toDate');
$toDate = DateTime::createFromString($toDate,   craft()-&gt;timezone) ;

//A bit of a hack, in my case these date fields are always the same anyway
$criteria-&gt;dateCreated = '&gt;= ' . $fromDate-&gt;format(DateTime::MYSQL_DATETIME);
$criteria-&gt;dateUpdated = '&lt;= ' . $toDate-&gt;format(DateTime::MYSQL_DATETIME);
</code></pre>

<p>I tried all sorts of ways to do it properly, things like</p>

<pre><code>$criteria-&gt;dateCreated ='and, &gt; ' . $fromDate-&gt;format(DateTime::MYSQL_DATETIME) . ', &gt; ' . $toDate-&gt;format(DateTime::MYSQL_DATETIME)'
</code></pre>

<p>anyone know how to do it properly?</p>

<plugin-development>
filtering elements between 2 dates
# filtering elements between 2 dates

(Asked by: username_0 on 2015-09-16)

I'm sure there must be an easy way to do this, but I can't work it out...
I'm trying to add date filters to the formerly submission export, but I can't work out how to filter out the submissions between 2 dates. I got it working with a bit of a hack:
$criteria = craft()->elements->getCriteria('Formerly_Submission');
$fromDate   = craft()->request->getPost('fromDate');
$fromDate = DateTime::createFromString($fromDate,   craft()->timezone) ;
$toDate   = craft()->request->getPost('toDate');
$toDate = DateTime::createFromString($toDate,   craft()->timezone) ;

//A bit of a hack, in my case these date fields are always the same anyway
$criteria->dateCreated = '>= ' . $fromDate->format(DateTime::MYSQL_DATETIME);
$criteria->dateUpdated = '<= ' . $toDate->format(DateTime::MYSQL_DATETIME);

I tried all sorts of ways to do it properly, things like
$criteria->dateCreated ='and, > ' . $fromDate->format(DateTime::MYSQL_DATETIME) . ', > ' . $toDate->format(DateTime::MYSQL_DATETIME)'

anyone know how to do it properly?

**Tags:** plugin-development

---

## 1 answers

### Answer by username_1 on 2015-09-16. Score: 4

You call it a bit of a hack, but I think the way you're doing it is the proper way:
$criteria->dateCreated = '>= ' . $fromDate->format(DateTime::MYSQL_DATETIME);
$criteria->dateUpdated = '<= ' . $toDate->format(DateTime::MYSQL_DATETIME);

Your last example would work (with a syntax change) if you were you looking for a date range within a single field (either dateCreated or dateUpdated) like so, but not across two fields:
$criteria->dateUpdated = array('and', '>='.$fromDate->format(DateTime::MYSQL_DATETIME), '<='.$toDate->format(DateTime::MYSQL_DATETIME));

> **Comments:**
> **username_0**: thanks brad, I knew I was close, thinking back I was probably doing something silly like having a < wrong way round  

---

