5

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?

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));
1
  • thanks brad, I knew I was close, thinking back I was probably doing something silly like having a < wrong way round Sep 20 '15 at 9:18

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.