1

Basic Sprout Report here, settings page:

{{ forms.dateTimeField({
  label: "Start Date"|t,
  instructions: "The start date of the date range you wish to display in your report."|t,
  id: 'datetimeStartDate',
  name: 'datetimeStartDate',
  value: settings.datetimeStartDate is defined ? settings.datetimeStartDate : now|date_modify("-30 days")
}) }}

{{ forms.dateTimeField({
  label: "End Date"|t,
  instructions: "The end date of the date range you wish to display in your report."|t,
  id: 'datetimeEndDate',
  name: 'datetimeEndDate',
  value: (settings.datetimeEndDate is defined ? settings.datetimeEndDate : now)
}) }}

Results page:

{% set purchases = craft.entries()
  .section('purchases')
  .limit(null)
%}

{% if settings.datetimeStartDate and settings.datetimeEndDate %}
  {% set purchases = purchases.postDate('and, >= ' ~ settings.datetimeStartDate.date ~ ', <= ' ~ settings.datetimeEndDate.date)
%}
{% endif %}

I am getting the following PHP error:

PHP Notice – yii\base\ErrorException Undefined property: DateTime::$date

I have copied the datetime fields directly from the example docs, and tested the results template code with hardcoded dates, which works fine, but passing dates this way does not work.

1 Answer 1

2

You can't access date this way, you'd need to use the date (docs) or datetime (docs) or even atom (docs) filters.

In the case of your report, since you have multiple conditions, you would need to pass those as an array and use the atom filter.

Try:

{% set purchases = purchases
    .postDate([
        'and', 
        '>=' ~ settings.datetimeStartDate|atom, 
        '<= ' ~ settings.datetimeEndDate|atom
    ]) %}

Alternatively, you can use before() (docs) and after() (docs) and pass date objects directly:

{% set purchases = purchases
    .after(settings.datetimeStartDate)
    .before(settings.datetimeEndDate) %}
6
  • I was working off the Sprout Reports documentation, which had this format settings.datetimeStartDate.date. I will try it with the standard Craft code as you noted.
    – 4midori
    Nov 12, 2020 at 17:41
  • Follow-up question, how would one output these values directly into a Sprout Report, that is, for testing? I can really only see if they work by outputting them either in the header row or a data row.
    – 4midori
    Nov 12, 2020 at 17:57
  • Sorry but I'm not too sure. Do the example templates help?
    – Oli
    Nov 13, 2020 at 12:32
  • @4midori If you want to output these values outside of a Sprout Report, you'll just need to do so in a test template. To test within Sprout Reports you'll need to create a simple header and data row and run the report. Nov 16, 2020 at 13:22
  • @BenParizek, could you provide more explicit instructions on how to do that? I assume that values like settings.whateverId are only set and retrieved within Sprout Report templates.
    – 4midori
    Nov 17, 2020 at 16:16

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.