1

I am trying to retrieve an array of entries on a particular section where inspectionDate is greater than today's date. Here is the code.

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'inspections';
$criteria->inspectionDate > '("now")';
$criteria->order = 'dateCreated asc';
$inspections = $criteria->find();

I am getting a Property "Craft\ElementCriteriaModel.inspectionDate" is not defined. error when trying to run this code.I have made sure that inspectionDate is the field handle and is also a date type.

2

It seems like a syntax error, you did not set the date correctly. This should work

    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = 'inspections';
    $criteria->inspectionDate = ">" . time();
    $criteria->order = 'dateCreated asc';
    $inspections = $criteria->find();
2

The Error was because

$criteria->inspectionDate > '("now")';

This way you can't get today's date.

You could try this:

$date = new DateTime();
$formattedDate = $date->format(DateTime::MYSQL_DATETIME);
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'inspections';
$criteria->inspectionDate = '> '.$formattedDate;
$criteria->order = 'dateCreated asc';
$inspections = $criteria->find();
  • im marking Robins answer as correct for simplicity sake, your answer works as well. – matthew Nov 29 '17 at 1:24

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.