3

I'm building an export plugin in Craft 2 and the entries are saved by the front-end and are never enabled, so no Post Date. I have a form with the forms.date selectors that sends a start and end date to my controller. How can I filter on Date Created? After and before only works on Post Date.

I have this for now:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'contestEntries';
// Date Created >= start_date
// Date Created <= end_date
$criteria->status = null;
$entries = $criteria->find();

Thank you

1 Answer 1

2

Here is syntax you can use to search a date field between two dates.

$startDate = date('Y-m-d', strtotime($settings["start_date"]["date"]));
$endDate = date('Y-m-d', strtotime($settings["end_date"]["date"]));

$criteria->dateCreated = ['and','>= '.$startDate, '<= '.$endDate];
3
  • Sorry, I don't think this works at all for what I'm asking. You could use this if the entries would have a start_date and an end_date field, but not for what I'm asking. I want to do this, but against the Date Created field, using a user input for the start and end date, pulling entries that are created between those 2 dates. Commented Sep 7, 2017 at 21:12
  • I have updated the answer.
    – a-am
    Commented Sep 7, 2017 at 21:21
  • Yes, it's almost what I ended up with, using your first example. Thanks for that. But just so you know, you can't use the DateTime object in this case, trying to concatenate it with a string. Just use $startDate = date('Y-m-d', strtotime($settings["start_date"]["date"])); $endDate = date('Y-m-d', strtotime($settings["end_date"]["date"])); If you can update your answer to reflect this, it will be perfect. Thanks for the help Commented Sep 7, 2017 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.