Craft CMS Asked on July 30, 2021
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 – yiibaseErrorException 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.
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) %}
Correct answer by Oli on July 30, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP